简体   繁体   English

Node.js:找不到模块'chai'

[英]Node.js: Cannot find module 'chai'

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 jade@0.26.3: Jade has been renamed to pug, please install th
e latest version of pug instead of jade
npm WARN deprecated graceful-fs@2.0.3: 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
mocha@2.4.5 C:\Users\TestUser\AppData\Roaming\npm\node_modules\mocha
├── escape-string-regexp@1.0.2
├── growl@1.8.1
├── commander@2.3.0
├── diff@1.4.0
├── supports-color@1.2.0
├── debug@2.2.0 (ms@0.7.1)
├── mkdirp@0.5.1 (minimist@0.0.8)
├── jade@0.26.3 (commander@0.6.1, mkdirp@0.3.0)
└── glob@3.2.3 (inherits@2.0.1, graceful-fs@2.0.3, minimatch@0.2.14)

Also installed chai (sorry, I originally forgot to mention it) 还装了柴(对不起,我原本忘了提它)

C:\Windows\system32>npm install -g chai
chai@3.5.0 C:\Users\TestUser\AppData\Roaming\npm\node_modules\chai
├── assertion-error@1.0.1
├── type-detect@1.0.0
└── deep-eql@0.1.3 (type-detect@0.1.1)

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? 我究竟做错了什么?

You have installed chai globally (with -g option), that's why require don't find it. 你已经全局安装了chai (使用-g选项),这就是为什么require找不到它。

You need to install it locally (on your node_modules directory), so that require can find it. 您需要在本地安装它(在node_modules目录下),以便require可以找到它。

To do so, type: 为此,请键入:

npm install --save-dev chai

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

npm install chai

你应该安装柴

$ npm install --save chai

$ npm install chai

chai@3.5.0 ../../../../../../node_modules/chai
├── assertion-error@1.0.2
├── type-detect@1.0.0
└── deep-eql@0.1.3 (type-detect@0.1.1)

-MacBook-Air:test $ mocha


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


  1 passing (9ms)

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 install chi

npm test (the tests were executed successfully) npm test(测试成功执行)

npm install types/chai npm安装类型/柴

Install in the local drive 安装在本地驱动器中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM