如何解决“找不到模块"使用 Node.js 时出错? [英] How do I resolve "Cannot find module" error using Node.js?

查看:45
本文介绍了如何解决“找不到模块"使用 Node.js 时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

> npm install ../faye

这似乎可以解决问题:

> npm list
/home/dave/src/server
└─┬ faye@0.7.1
  ├── cookiejar@1.3.0
  ├── hiredis@0.1.13
  └── redis@0.7.1

但是 Node.js 找不到模块:

But Node.js can't find the module:

> 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?

推荐答案

使用 npm install 仅将模块安装到当前目录(在名为 node_modules 的子目录中).app.js 是否位于 home/dave/src/server/ 下?如果没有,并且您想从任何目录使用该模块,则需要使用 npm install -g 全局安装它.

Using npm install installs the module into the current directory only (in a subdirectory called node_modules). Is app.js located under 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.

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

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

更新 (8/2019):

现在你可以使用 package-lock.json 文件,它会自动当 npm 修改您的 node_modules 目录时生成.因此,您可以不签入包,因为 package-lock.json 会跟踪您当前正在使用的 node_modules 的确切版本.要从 package-lock.json 而不是 package.json 安装包,请使用命令 npm ci.

Nowadays you can use package-lock.json file, which is automatically generated when npm modifies your node_modules directory. 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. To install packages from package-lock.json instead of package.json use the command npm ci.

更新 (3/2016):

我的回复收到了很多批评,特别是我检查了我的代码所依赖的包.几天前,有人取消发布他们所有的包(https://kodfabrik.com/journal/i-ve-just-liberated-my-modules) 破坏了 React、Babel 和几乎所有其他内容.希望现在很清楚,如果你有生产代码,你不能依赖 NPM 实际上为你维护你的依赖项.

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://kodfabrik.com/journal/i-ve-just-liberated-my-modules) which broke React, Babel, and just about everything else. Hopefully it's clear now that if you have production code, you can't rely on NPM actually maintaining your dependencies for you.

这篇关于如何解决“找不到模块"使用 Node.js 时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆