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
1 Answer
Reset to default 12What 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 ashttp
; 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
版权声明:本文标题:javascript - Node.js require() returns undefined always? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741268546a2368899.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论