与npm脚本混淆

时间:2018-04-17 19:35:06

标签: node.js npm

我想知道https://github.com/fastify/fastify-example-twitter/blob/master/package.json是如何工作的。

具体来说,它的脚本条目为:"start": "fastify index.js"

但是,无需在全球范围内安装fastify。尽管如此,npm start工作正常。它确实从fastify开始,而从shell执行此操作会导致:-bash: fastify: command not found

调用npm start时发生了什么?为什么我不能从命令行运行它,而npm运行此脚本命令就好了。

2 个答案:

答案 0 :(得分:2)

如果您的依赖项出现在npm脚本命令中,则可执行文件将添加到您的路径中。

来自npm scripts documentation

  

如果您依赖于定义可执行脚本的模块,例如test   套件,然后将那些可执行文件添加到PATH中以便执行   脚本。所以,如果你的package.json有这个:

{ 
    "name" : "foo" , 
    "dependencies" : { 
      "bar" : "0.1.x" 
    } , 
    "scripts": {
      "start" : "bar ./test" 
    } 
}
     

然后你可以运行npm start来执行bar脚本,这是   导出到npm install上的node_modules / .bin目录。

答案 1 :(得分:0)

fastify被列为依赖项,因此可以作为npm脚本运行。 mochastandardsnazzy也是如此。它们都不需要全局安装,而是通过他们的npm脚本运行。