如何使用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
└─┬ faye@0.7.1
  ├── cookiejar@1.3.0
  ├── hiredis@0.1.13
  └── redis@0.7.1

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? 有什么建议么?


#1楼

参考:https://stackoom.com/question/brT6/如何使用Node-js解决-找不到模块-错误


#2楼

I had a very similar issue. 我有一个非常相似的问题。 Removing the entire node_modules folder and re-installing worked for me: 删除整个node_modules文件夹并重新安装对我node_modules

rm -rf node_modules
npm install

#3楼

Change the directory and point to your current project folder and then "npm install". 更改目录并指向您当前的项目文件夹,然后单击“ npm install”。 .

This will install all dependencies and modules into your project folder. 这会将所有依赖项和模块安装到您的项目文件夹中。


#4楼

npm install --save module_name

For example, if the error is: 例如,如果错误是:

{ [Error: Cannot find module '/root/.npm/form-data'] code: 'MODULE_NOT_FOUND' } {[错误:找不到模块'/root/.npm/form-data']代码:'MODULE_NOT_FOUND'}

then you can resolve this issue by executing the command npm install --save form-data . 然后您可以通过执行命令npm install --save form-data解决此问题。


#5楼

This happens when a first npm install has crashed for some reason (SIGINT of npm), or that the delay was too long, or data is corrupted. 当第一次npm安装由于某种原因(npm的SIGINT)而崩溃,延迟时间过长或数据损坏时,就会发生这种情况。 Trying an npm install again won't save the problem. 再次尝试安装npm不会解决问题。

Something got wrong on the npm first check, so the best choice is to remove the file and to restart npm install. 在npm第一次检查时出了点问题,因此最好的选择是删除文件并重新启动npm安装。


#6楼

I was trying to publish my own package and then include it in another project. 我试图发布自己的程序包,然后将其包含在另一个项目中。 I had that issue because of how I've built the first module. 我遇到了这个问题,因为我是如何构建第一个模块的。 Im using ES2015 export to create the module, eg lets say the module looks like that: 我使用ES2015导出来创建模块,例如,假设模块如下所示:

export default function(who = 'world'){
    return `Hello ${who}`;
}

After compiled with Babel and before been published: 在与Babel一起编译之后,在发布之前:

'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});

exports.default = function () {
    var who = arguments.length <= 0 || arguments[0] === undefined ? 'world' : arguments[0];


    return 'Hello ' + who;
};

So after npm install module-name in another project (none ES2015) i had to do 所以在npm install module-name在另一个项目中npm install module-name之后(没有ES2015),我不得不做

var hello = require('module-name').default;

To actually got the package imported. 实际要导入的包。

Hope that helps! 希望有帮助!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值