Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

node.js - 如何使用Node.js解决“找不到模块”错误?(How do I resolve “Cannot find module” error using Node.js?)

After pulling down a module from GitHub and following the instructions to build it, I try pulling it into an existing project using:

(从GitHub提取模块并按照说明进行构建后,我尝试使用以下方法将其拖入现有项目:)

> npm install ../faye

This appears to do the trick:

(这似乎可以解决问题:)

> npm list
/home/dave/src/server
└─┬ [email protected]
  ├── [email protected]
  ├── [email protected]
  └── [email protected]

But Node.js can't find the module:

(但是Node.js找不到模块:)

> node app.js
node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: Cannot find module 'faye'
    at Function._resolveFilename (module.js:334:11)
    at Function._load (module.js:279:25)
    at Module.require (module.js:357:17)
    at require (module.js:368:17)
    at Object.<anonymous> (/home/dave/src/server/app.js:2:12)
    at Module._compile (module.js:432:26)
    at Object..js (module.js:450:10)
    at Module.load (module.js:351:31)
    at Function._load (module.js:310:12)
    at Array.0 (module.js:470:10)

I really want to understand what is going on here, but I'm at a bit of a loss as to where to look next.

(我真的很想了解这里发生了什么,但是我对接下来的去向有些困惑。)

Any suggestions?

(有什么建议么?)

  ask by Dave Causey translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using npm install installs the module into the current directory only (in a subdirectory called node_modules ).

(使用npm install将模块安装到当前目录中(在名为node_modules的子目录中)。)

Is app.js located under home/dave/src/server/ ?

(app.js是否位于home/dave/src/server/ ?)

If not and you want to use the module from any directory, you need to install it globally using npm install -g .

(如果不是,并且您想从任何目录使用该模块,则需要使用npm install -g全局npm install -g 。)

I usually install most packages locally so that they get checked in along with my project code.

(我通常在本地安装大多数软件包,以便它们与我的项目代码一起签入。)

Update (8/2019):

(更新(8/2019):)

Nowadays you can use package-lock.json file, which is automatically generated when npm modifies your node_modules directory.

(如今,您可以使用package-lock.json文件,该文件在npm修改您的node_modules目录时自动生成。)

Therefore you can leave out checking in packages, because the package-lock.json tracks the exact versions of your node_modules, you're currently using.

(因此,您package-lock.json签入包,因为package-lock.json跟踪您当前正在使用package-lock.json的确切版本。)

To install packages from package-lock.json instead of package.json use the command npm ci .

(要从package-lock.json而不是package.json安装软件包,请使用命令npm ci 。)

Update (3/2016):

(更新(3/2016):)

I've received a lot of flak for my response, specifically that I check in the packages that my code depends on.

(我收到了很多回应,特别是我签入了我的代码所依赖的软件包。)

A few days ago, somebody unpublished all of their packages ( https://medium.com/@azerbike/i-ve-just-liberated-my-modules-9045c06be67c#.kq9s64clp ) which broke React, Babel, and just about everything else.

(几天前,有人取消发布了所有软件包( https://medium.com/@azerbike/i-ve-just-liberated-my-modules-9045c06be67c#.kq9s64clp ),这破坏了React,Babel和几乎所有东西其他。)

Hopefully it's clear now that if you have production code, you can't rely on NPM actually maintaining your dependencies for you.

(希望现在很清楚,如果您具有生产代码,则不能依赖NPM来真正为您维护依赖项。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...