简体   繁体   English

NPM-脚本-它们如何工作?

[英]NPM - Scripts - How Do They Work?

I cant get my head around how scripts are running within package.json & would appreciate some insight for us newbies. 我无法了解如何在package.json中运行脚本,并且希望对我们的新手有所帮助。

Is it the case that they are bash scripts that are run by node having loaded the various dependencies ? 是否是由加载了各种dependencies节点运行的bash脚本?

If yes, then how does it process the javascript code? 如果是,那么它将如何处理javascript代码?

Is it the case that they are bash scripts 是bash脚本吗?

yes

that are run by node 由节点运行

no, they are run by sh . 不,他们由sh

having loaded the various dependencies? 已经加载了各种依赖项?

no, no js files are loaded, the only thing npm does for you is to prepare the environment . 不,不加载任何js文件, npm唯一为您做的就是准备环境 Among other things, it adds ./node_modules/.bin to PATH so you can invoke installed modules immediately. 除其他功能外,它./node_modules/.bin添加到PATH以便您可以立即调用已安装的模块。

When you run npm run-script whatever , this is what npm does: 当您运行npm run-script whatever ,这就是npm作用:

  • reads the corresponding command line from package.json package.json读取相应的命令行
  • prepares the environment 准备环境
  • invokes sh (or comspec on win) and gives it the command and the env. 调用sh (或comspec上的comspec ),并为其提供命令和环境。 No big magic here. 这里没有大魔术。

This may not be 100% accurate so I implore other, more qualifies, experts to chime in. 这可能不是100%准确的,所以我恳请其他,更有资格的专家加入。

NPM is a program, installed as part of the Node.JS environment. NPM是作为Node.JS环境的一部分安装的程序。 It's two main uses (as describe here ) are for searching for node.js packages and installing node.js packages. 这是两种主要用途(如描述这里 )是用于搜索node.js的包和安装的node.js包。

However, NPM is also capable of understanding "simple" (a relative term) scripts. 但是,NPM也能够理解“简单”(相对术语)脚本。

When you write a script in your package.json, and issue the NPM command, say "npm start", NPM will read and interpret the script. 当您在package.json中编写脚本并发出NPM命令时,说“ npm start”,NPM将读取并解释该脚本。 NPM then searches your node_modules structure for the accompanying binary and executes that binary with the necessary start parameters. 然后NPM在您的node_modules结构中搜索随附的二进制文件,并使用必要的启动参数执行该二进制文件。

An example would be 一个例子是

"test": "mocha --reporter spec test"

when you issue "npm test", NPM will look for the mocha binary in your node_modules structure. 当您发出“ npm test”时,NPM将在node_modules结构中查找摩卡二进制文件。 NPM finds mocha initiates the call, passing the reporter command arg (--reporter spec) and the name of the file to be read and executed for the test. NPM发现mocha发起了调用,传递了报告程序命令arg(-reporter spec)以及要读取和执行的测试文件的名称。

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

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