根据节点js说明手册,强烈建议不要全局安装任何节点模块。
我尝试过各种各样的事情。我在我的主目录,我的git文件(带有node_modules)文件夹和实际的node_modules文件夹中执行了以下命令。
> var express=require('express');
undefined
> var express=require('node_modules/express');
Error: Cannot find module 'node_modules/express'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at repl:1:13
at REPLServer.self.eval (repl.js:110:21)
at repl.js:249:20
at REPLServer.self.eval (repl.js:122:7)
at Interface.<anonymous> (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)
请注意,在上面,我期望它们在我的git文件夹中(带有node_modules)。 我发现这条线很好奇
> var express=require('express');
产生
undefined
在主目录中,我只是得到了相同的“找不到模块”错误。
答案 0 :(得分:29)
您的require
声明工作正常。忽略undefined
,这只是节点REPL。我会解释undefined
here,并参阅下面的评论,以获取有关该附加材料的链接。
您可以通过以下方式验证:
mkdir /tmp/test-repl
cd /tmp/test-repl
npm install express
node
> var express = require('express');
undefined
> express
//long object which is the express module gets printed
答案 1 :(得分:4)
打印undefined
是浏览器控制台和节点repl的正常行为。
尝试输入:express。 (tab键) - 你应该得到这样的东西:
> var express = require('express');
undefined
> express.
express.__defineGetter__ express.__defineSetter__ express.__lookupGetter__ express.__lookupSetter__ express.constructor express.hasOwnProperty
express.isPrototypeOf express.propertyIsEnumerable express.toLocaleString express.toString express.valueOf
express.apply express.arguments express.bind express.call express.caller express.constructor
express.length express.name express.toString
express.Route express.Router express.application express.arguments express.basicAuth express.bodyParser
express.caller express.compress express.cookieParser express.cookieSession express.createServer express.csrf
express.directory express.errorHandler express.favicon express.json express.length express.limit
express.logger express.methodOverride express.mime express.multipart express.name express.prototype
express.query express.request express.response express.responseTime express.session express.static
express.staticCache express.timeout express.urlencoded express.version express.vhost
答案 2 :(得分:2)