admin管理员组

文章数量:1279086

I am trying to use Node.js with Amazon AWS and when I try to declare an aws instance I keep getting returned undefined. Also when I try to require a mon module such as http, terminal also returns undefined. This occurs when I try to execute my actual script.

Terminal Snippet:

User$ node

> var aws=require('aws-sdk')
    undefined

> var web =require('http')
undefined

I am trying to use Node.js with Amazon AWS and when I try to declare an aws instance I keep getting returned undefined. Also when I try to require a mon module such as http, terminal also returns undefined. This occurs when I try to execute my actual script.

Terminal Snippet:

User$ node

> var aws=require('aws-sdk')
    undefined

> var web =require('http')
undefined
Share Improve this question edited Feb 14, 2016 at 8:58 Bruno Reis 37.8k13 gold badges123 silver badges157 bronze badges asked Feb 14, 2016 at 8:53 Mohammed BMohammed B 3051 gold badge4 silver badges16 bronze badges 1
  • 2 FYI @NoyGabay - if the dependencies didn't exist, the error would've been very different – Jaromanda X Commented Feb 14, 2016 at 9:25
Add a ment  | 

1 Answer 1

Reset to default 12

What you are seeing is not the return value of require(...), simply because that's not what you have typed.

You are observing the result of the statement, var aws = require('aws-sdk'). And that statement, a variable declaration with an assignment, has an "undefined value". If you inspect what has been stored in the aws variable, you'll see that it is not undefined, it contains the module returned by the require(...) call.

Try this:

  • start node
  • type var x = 2

You'll also see undefined. And you know that "2" is definitely not "undefined".

Now, try this:

  • start node
  • type require('aws-sdk') (or any other module, such as http; note that this is just requiring the module, not assigning it to any variable)

You'll see the module being printed in the REPL.

Finally, try this:

  • start node
  • type var aws = require('aws-sdk')
  • the type aws

This will print the value of the aws variable into the REPL. And that value is whatever has been returned by the require(...) call. And you'll see that it's definitely not "undefined".

This is the precisely expected behavior of Node.js in whatever platform (i.e., what you are observing is pletely unrelated to the fact that you are running Node on AWS; you could run it on your laptop, whatever OS you have, and you'd see the exact same behavior).

本文标签: javascriptNodejs require() returns undefined alwaysStack Overflow