在与此问题类似的情况下,我使用 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')