2

在与此问题类似的情况下,我使用 npm 安装了一个模块,但require('module')找不到它。

my-machine:projectname me$ npm install express-csrf
npm info it worked if it ends with ok
npm info version 0.2.1
npm info install express-csrf@0.3.3
npm info activate express-csrf@0.3.3
npm info build Success: express-csrf@0.3.3
npm ok

到现在为止还挺好。但是之后...

my-machine:projectname me$ node
> require('express-csrf')
Error: Cannot find module 'express-csrf'
at Function._resolveFilename (module.js:317:11)
at Function._load (module.js:262:25)
at require (module.js:346:19)
at [object Context]:1:1
at Interface.<anonymous> (repl.js:171:22)
at Interface.emit (events.js:64:17)
at Interface._onLine (readline.js:153:10)
at Interface._line (readline.js:408:8)
at Interface._ttyWrite (readline.js:585:14)
at ReadStream.<anonymous> (readline.js:73:12)

npm 说包在那里:

my-machine:projectname me$ npm ls express-csrf
npm info it worked if it ends with ok
npm info version 0.2.1
express-csrf@0.3.3             active installed
npm ok

请注意,新模块不会出现在 /usr/local/lib/node 目录中,但它确实出现在 /usr/local/lib/node/.npm 下

my-machine:projectname me$ ls /usr/local/lib/node/express-csrf*
ls: /usr/local/lib/node/express-csrf*: No such file or directory
my-machine:projectname me$ ls /usr/local/lib/node/.npm/express-csrf*
0.3.3   active

更新:我应该提到,当我完全限定require语句中的路径时,一切正常,如下所示:

require('/usr/local/lib/node/.npm/express-csrf/active/package/index.js')
4

2 回答 2

2

升级 npm。

您的问题是 require() 不搜索 .npm 文件夹。

您可以通过升级 npm 来解决此问题,以便将文件放置在正确的位置。或者,您可以在项目根目录中的文件夹中手动安装模块并 require('../mymodule')

于 2011-11-14T08:32:51.537 回答
0

如果您在 Windows 上,请使用:

导出 NODE_PATH="C:\Users\IMarek\AppData\Roaming\npm\node_modules"
于 2012-09-17T21:21:04.517 回答