Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 602 Vote(s) - 3.41 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending command line arguments to npm script

#21
## npm 2 and newer ##

It's possible to [pass args to `npm run`](

[To see links please register here]

) since npm 2 (2014). The syntax is as follows:

`npm run <command> [-- <args>]`

Note the `--` separator, used to separate the params passed to `npm` command itself, and the params passed to your script.

With the example `package.json`:

```json
"scripts": {
"grunt": "grunt",
"server": "node server.js"
}
```

here's how to pass the params to those scripts:

```sh
npm run grunt -- task:target // invokes `grunt task:target`
npm run server -- --port=1337 // invokes `node server.js --port=1337`
```

_Note_: **If your param does not start with `-` or `--`, then having an explicit `--` separator is not needed; but it's better to do it anyway for clarity.**

```sh
npm run grunt task:target // invokes `grunt task:target`
```

Note below the difference in behavior (`test.js` has `console.log(process.argv)`): the params which start with `-` or `--` _are passed to `npm` and not to the script_, and are silently swallowed there.

```sh
$ npm run test foobar
['C:\\Program Files\\nodejs\\node.exe', 'C:\\git\\myrepo\\test.js', 'foobar']

$ npm run test -foobar
['C:\\Program Files\\nodejs\\node.exe', 'C:\\git\\myrepo\\test.js']

$ npm run test --foobar
['C:\\Program Files\\nodejs\\node.exe', 'C:\\git\\myrepo\\test.js']

$ npm run test -- foobar
['C:\\Program Files\\nodejs\\node.exe', 'C:\\git\\myrepo\\test.js', 'foobar']

$ npm run test -- -foobar
['C:\\Program Files\\nodejs\\node.exe', 'C:\\git\\myrepo\\test.js', '-foobar']

$ npm run test -- --foobar
['C:\\Program Files\\nodejs\\node.exe', 'C:\\git\\myrepo\\test.js', '--foobar']
```

The difference is clearer when you use a param _actually used by npm_:

```sh
$ npm test --help // this is disguised `npm --help test`
npm test [-- <args>]

aliases: tst, t
```

To get the parameter value, [see this question](

[To see links please register here]

). For reading named parameters, it's probably best to use a parsing library like [yargs](

[To see links please register here]

) or [minimist](

[To see links please register here]

); nodejs exposes `process.argv` globally, containing command line parameter values, but this is a low-level API (whitespace-separated array of strings, as provided by the operating system to the node executable).
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through