Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does -- do when running an npm command?

Tags:

bash

npm

As an example, double dash or two hyphens -- is used like so:

npm test -- --coverage

Running npm without the double dash flag does not run in coverage mode so it seems to append subsequent flags, is this correct? I couldn't find the documentation on this.

like image 618
Kunal Avatar asked Mar 27 '17 12:03

Kunal


People also ask

What does npm Run command do?

npm run sets the NODE environment variable to the node executable with which npm is executed. If you try to run a script without having a node_modules directory and it fails, you will be given a warning to run npm install , just in case you've forgotten.

What happens when you run npm start?

npm start: npm start script is used to execute the defined file in it without typing its execution command.

How do I run a npm file?

You can easily run scripts using npm by adding them to the "scripts" field in package. json and run them with npm run <script-name> . Run npm run to see available scripts. Binaries of locally install packages are made available in the PATH , so you can run them by name instead of pointing to node_modules/.

What happens when you run npm install?

When npm install is run with a specified package argument, it installs the package in the existing node_modules directory. You can optionally provide a specific version as well... When a version is not provided, npm automatically downloads the latest stable version.


2 Answers

-- as an argument on its own is standardized across all UNIX commands: It means that further arguments should be treated as positional arguments, not options. See Guideline 10 in POSIX Utility Syntax Conventions.

To give you a non-NPM-based example, ls -- -l will look for a file named -l, because the -- specified that all subsequent arguments are positional.

In this context, it means that --coverage isn't an option to npm itself; presumably, then, it's subsequently read by the test subcommand. For a tool that were following the conventions properly this wouldn't be necessary, because Guideline 9 specifies that all options shall be given before any arguments (thus that in this context --coverage should be treated as an argument since it comes after the argument test); however, inasmuch as NPM is only partially following the guidelines, this is a foreseeable result.

(Long --option-style options are actually a GNU extension as a whole, so what we have here is a mismash of multiple parsing styles; such is life, unfortunately).

like image 129
Charles Duffy Avatar answered Oct 07 '22 05:10

Charles Duffy


I've done some further digging; according to the docs for my node version -

"--" Indicates the end of node options. Pass the rest of the arguments to the script. If no script filename or eval/print script is supplied prior to this, then the next argument will be used as a script filename.

But, a simple script that contains -

console.log(`process.execArgv:${process.execArgv}`);
console.log(`process.argv:${process.argv}`);

behaves as -

>node --prof argv.js --myArg
process.execArgv:--prof
process.argv:C:\Program Files\nodejs\node.exe,C:\Dev\Web\QA_Web_POC\argv.js,--myArg

>node --prof argv.js -- --myArg
process.execArgv:--prof
process.argv:C:\Program Files\nodejs\node.exe,C:\Dev\Web\QA_Web_POC\argv.js,--, --myArg

>node argv.js --prof -- --myArg
process.execArgv:
process.argv:C:\Program Files\nodejs\node.exe,C:\Dev\Web\QA_Web_POC\argv.js,--prof,--,--myArg

>node argv.js -- --prof --myArg
process.execArgv:
process.argv:C:\Program Files\nodejs\node.exe,C:\Dev\Web\QA_Web_POC\argv.js,--,--prof,--myArg

So, it seems there's a bug?

like image 1
martinp999 Avatar answered Oct 07 '22 04:10

martinp999