【问题标题】:In node, why does require() fail to find a module I just installed with npm?在 node 中,为什么 require() 找不到我刚刚用 npm 安装的模块?
【发布时间】:2011-12-28 08:12:40
【问题描述】:

在一个看起来类似于this problem, 的情况下,我已经使用 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')

【问题讨论】:

标签: node.js npm


【解决方案1】:

如果你在 Windows 上使用:

export NODE_PATH="C:\Users\IMarek\AppData\Roaming\npm\node_modules"

【讨论】:

  • 您能否在答案中发布您博客文章的相关部分?而不是在几个答案上发布链接。
【解决方案2】:

升级 npm。

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

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

【讨论】:

  • 谢谢,克里斯。我会升级,如果不行,我会尝试手动安装那个有问题的模块。
猜你喜欢
  • 1970-01-01
  • 2021-04-28
  • 2020-12-04
  • 1970-01-01
  • 2019-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多