简体   繁体   English

“npm”如何运行“npm test”?

[英]How does “npm” run “npm test”?

I always thought that npm test command just launches what I would write in package.json inside scripts: { test: ...} section. 我一直认为npm test命令只是启动我在package.json里面写的scripts: { test: ...}部分。 But I have this weird bug when it doesn't work. 但是当它不起作用时,我有这个奇怪的错误。

So, I have this piece of config in package.json 所以,我在package.json有这个配置

"scripts": {
  "start": "node index.js",
  "test": "mocha tests/spec.js"
}

When I try to run tests I type npm test in terminal and had this error: 当我尝试运行测试时,我在终端中键入npm test并出现此错误:

module.js:340
    throw err;
          ^
Error: Cannot find module 'commander'

But everything is OK when I type just mocha tests/spec.js . 但是当我输入mocha tests/spec.js时,一切都mocha tests/spec.js Any ideas why is that? 任何想法为什么会这样?

UPDATE: 更新:

I've tried to install commander and I had an error Cannot find module 'glob' . 我试图安装指挥官,我有一个错误找不到模块'glob' After installing glob I have 安装完成后glob我有

Error: Cannot find module '../'** 错误:找不到模块'../'**

But actually question is why do I have these errors and why is everything OK when running mocha tests/spec.js ? 但实际问题是为什么我有这些错误,为什么运行mocha tests/spec.js时一切正常?

You may have two versions of mocha installed: one globally ( npm install -g mocha ) and one locally, which appears to be broken. 您可能安装了两个版本的mocha:一个是全局的( npm install -g mocha ),另一个是本地的,看起来很糟糕。

When you run a script through npm , either as npm run-script <name> or with a defined shortcut like npm test or npm start , your current package directory's bin directory is placed at the front of your path. 当您通过npm运行脚本时,无论是npm run-script <name>还是使用npm testnpm start等已定义的快捷方式,您当前的包目录的bin目录都会放在路径的前面。 For your package that's probably ./node_modules/.bin/ , which contains a link to your package's mocha executable script. 对于你的包可能是./node_modules/.bin/ ,它包含你的包的mocha可执行脚本的链接。

You can probably fix this by removing the local mocha and reinstalling it with --save-dev: 您可以通过删除本地摩卡并使用--save-dev重新安装它来解决此问题:

rm -rf node_modules/mocha
npm install --save-dev mocha

That should get you a working local copy of mocha with all its dependencies (commander etc.) installed. 这应该会让你获得一个工作的mocha本地副本及其所有依赖项(指挥官等)。

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

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