Javascript Node.js:找不到模块“chai”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36838834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:37:33  来源:igfitidea点击:

Node.js: Cannot find module 'chai'

javascriptnode.jsnpm

提问by aaa

I am trying to follow a first example from https://mochajs.org/

我正在尝试遵循https://mochajs.org/的第一个示例

Done this

做这个

$ npm install -g mocha

Got

得到了

C:\Windows\system32>npm install -g mocha
npm WARN deprecated [email protected]: Jade has been renamed to pug, please install th
e latest version of pug instead of jade
npm WARN deprecated [email protected]: graceful-fs version 3 and before will fai
l on newer node releases. Please update to graceful-fs@^4.0.0 as soon as possibl
e.
C:\Users\TestUser\AppData\Roaming\npm\_mocha -> C:\Users\TestUser\AppData\Roamin
g\npm\node_modules\mocha\bin\_mocha
C:\Users\TestUser\AppData\Roaming\npm\mocha -> C:\Users\TestUser\AppData\Roaming
\npm\node_modules\mocha\bin\mocha
[email protected] C:\Users\TestUser\AppData\Roaming\npm\node_modules\mocha
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
└── [email protected] ([email protected], [email protected], [email protected])

Also installed chai (sorry, I originally forgot to mention it)

还装了chai(不好意思,原来忘记说了)

C:\Windows\system32>npm install -g chai
[email protected] C:\Users\TestUser\AppData\Roaming\npm\node_modules\chai
├── [email protected]
├── [email protected]
└── [email protected] ([email protected])

And here is the code

这是代码

var assert = require('chai').assert;
describe('Array', function() {
  describe('#indexOf()', function () {
    it('should return -1 when the value is not present', function () {
      assert.equal(-1, [1,2,3].indexOf(5));
      assert.equal(-1, [1,2,3].indexOf(0));
    });
  });
});

Keep getting

不断获得

Error: Cannot find module 'chai'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (c:\git\develop\SendText\test\test2.js:1:76)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)

What am I doing wrong?

我究竟做错了什么?

回答by Blackus

You have installed chaiglobally(with -goption), that's why requiredon't find it.

您已chai全局安装(带-g选项),这就是为什么require找不到它。

You need to install it locally(on your node_modulesdirectory), so that requirecan find it.

您需要在本地(在您的node_modules目录中)安装它,以便require可以找到它。

To do so, type:

为此,请键入:

npm install --save-dev chai

回答by Анатолий Смакаев

You need to install chailocally to require it.

您需要在chai本地安装才能使用它。

npm install chai

回答by Syam Pillai

You should install chai

你应该安装柴

$ npm install --save chai

$ npm install --save chai

回答by wei

$ npm install chai

[email protected] ../../../../../../node_modules/chai
├── [email protected]
├── [email protected]
└── [email protected] ([email protected])

-MacBook-Air:test $ mocha


  Array
    #indexOf()
      ? should return -1 when the value is not present


  1 passing (9ms)

回答by Jose Luis Choque Chavez

You need to be sure that both installations are on the same folder because if the node_modules folder (created during mocha installation) is not on the same folder where you are installing chi, then the error: Cannot find module 'chai' appears.

您需要确保两个安装都在同一文件夹中,因为如果 node_modules 文件夹(在 mocha 安装期间创建)不在您安装 chi 的同一文件夹中,则会出现错误:找不到模块 'chai'。

npm install --global mocha

npm install --global mocha

npm install chi

npm 安装 chi

npm test (the tests were executed successfully)

npm test(测试成功执行)

回答by San Jaisy

npm install types/chai

npm 安装类型/柴

Install in the local drive

安装在本地驱动器中