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

查看:133
本文介绍了如何解决“找不到模块"问题? 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):

Update (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):

Update (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天全站免登陆