Let's say I have the following in a file called print-last-arg.js:
console.log(process.argv[process.argv.length-1])
And the following scripts in my package.json:
"scripts": {
"print_a": "node print-last-arg.js",
"print_b": "npm run print_a"
}
When I run npm run print_a -- --foo=bar, I get --foo=bar as expected.
However, npm run print_b -- --foo=bar gives me no output.
How do I pass the CLI arguments from print_b to print_a?
It turns out that you just have to add an extra -- on the end of print_b, which will tell npm to pass whatever arguments print_b got to print_a. So,
"scripts": {
"print_a": "node print-last-arg.js",
"print_b": "npm run print_a"
}
becomes
"scripts": {
"print_a": "node print-last-arg.js",
"print_b": "npm run print_a -- "
}
VoilĂ ! Now npm run print_b -- --foo=bar prints --foo=bar as expected.
Related
I want to run a npm script which accepts optional params, and has a fallback for a default values for params that were not passed,
for example:
I have this "example" script defined in my pacakge.json:
"scripts": {
"example": "echo input"
},
I want to pass in the "input" as a parameter meaning:
If I run npm run example --input=true then it'll output true
If I run npm run example without params then it'll output false
I modified my pacakge.json to accept the input parameter:
"scripts": {
"example": "echo %npm_config_input%"
},
now when I run npm run example --input=true or npm run example --input=false
I get 'true' or 'false' respectively, but when I run npm run example the result I get is %npm_config_input%.
I want to give the param a default value so that when its not passed in explicitly - the default value will be used, something similar to :
"scripts": {
"example": "echo %npm_config_input% || 'false'"
},
So that running npm run example will result in 'false'
Is this possible?
You can achieve it using the https://www.npmjs.com/package/#naholyr/cross-env package, which supports default values for variables. On top of that, it will make your script OS-agnostic. Here's how:
"scripts": {
"example": "cross-env INPUT=${npm_config_input:false} npm run _example",
"_example": "cross-env command $INPUT"
},
For a reason yet unknown to me, using echo as the command will not print the variable value. But I'm successfully using this pattern to run sass with arguments, and fallback values, if no arguments are passed.
How can I pass arguments from one script to the other
using npm-run-all (to run in parallel)
scripts:{
"start":"run-p dev watch -- --theme=$themeId",
"dev": "webpack",
"watch": "theme watch --env=$themeId"
}
Then calling start like:
npm run start theme=2233
But it wouldn't pass the argument to the watch argument
Using ${npm_config_themeId}
"scripts": {
"start": "run-p dev watch",
"watch": "theme watch --env=${npm_config_theme}",
}
Then calling
npm run start -theme=123
I have some npm scripts. Some of them use env variables. For example (just example):
"scripts": {
"start": "webpack --watch --env.noSourceMaps",
},
So, for now, when I call npm start env.noSourceMaps = true
But, how can I unset that variable when run npm start?
If I run npm run -- --env.noSourceMaps=false I got env.noSourceMaps = [true, 'false']
If I run npm run -- --env.noSourceMaps I got env.noSourceMaps = [true, true]
If I run npm run -- --env.noSourceMaps= I got env.noSourceMaps = [true, '']
But I want env.noSourceMaps = false or undefined
And I do not want to reverse logic (I mean, i don't want to use it as "start": "webpack --watch" and call it as npm start or npm start -- --env.noSouceMaps or "start": "webpack --watch", "start:noMaps": "webpack --watch --env.noSourceMaps")
I'm using npm#5.6.0 on Mac High Sierra. I want to run tests that were setup in this Stratum client project. I have run npm install successfully. But when I try and run individual tests, I get the error:
no test specified
What gives? I am in the root project directory and the tests are in my "test" folder. Here is what happens:
localhost:stratum-client-master davea$ npm install
up to date in 0.381s
localhost:stratum-client-master davea$ npm test test/callbacks.js
> stratum-client#1.0.1 test /Users/davea/Documents/workspace/stratum-client-master
> echo "Error: no test specified" && exit 1 "test/callbacks.js"
Error: no test specified
sh: line 0: exit: too many arguments
npm ERR! Test failed. See above for more details.
Try replacing
"scripts": {
"test": "mocha"
},
in your package.json.
You're outputting exactly what the package.json file was told to output. Take a peek under scripts.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"int-test": "mocha --require babel-core/register --recursive test"
},
Try int-test, the other command in there.
Update: The package link has changed to the following and mocha should be the default test suite. You can run the other script with npm run bump-version; the original script above can be run with npm run int-test.
"scripts": {
"test": "mocha --recursive test",
"bump-version": "npm version patch"
},
You didn't specify which testing framework you're using such as Jest or Mocha.
In case of Jest, add this in your package.json:
"scripts" : {
"test" : "jest"
}
In the case of mocha refer to #Divyeshpal's answer.
The error can be for "exit 1"
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Change it to this:
"scripts": {
"test": "echo \"No test specified\""
},
This change works because exit 1 creates an error.
if you are using Jest and enzyme for unit testing and you have a jest.config.json config file, you can update your package.json under scripts to the following:
"scripts":{
"test": "jest --config=./jest.config.json --coverage",
}
The error can be for "exit 1"
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Change it to this:
"scripts": {
"test": "echo \"No test specified\""
},
This change worked for me because exit 1 created the error.
find and make changes in your package.json
"scripts":{
"test":"mocha"
}
add this on your packages.json file:
"scripts":{
"test": "mocha --ui tdd tests/callbacks.js",
}
then npm test on the console.
Replace
echo \"Error: no test specified\" && exit 1
in your
package.json
with
mocha.
I am trying to pass the parameters from a npm script to another (that is called by the first one), but I absolutely can't figure out how to do it.
Here is the case, I have the following scripts section in my package.json :
{
"scripts": {
"one": "npm run two && npm run three",
"two": "gulp build",
"three": "another random command"
}
}
I'm running script one like this : npm run one -- --arg=value. But I want to dynamically pass down arg to the script two.
To sum up, what I want is :
I type npm run one -- --arg=value
It runs npm run two -- --arg=value && npm run three
Which results in running gulp build --arg=value, followed by the other random command
Does anybody have an idea ? Thank you very much.
All three methods below work with npm run one -- --arg=value.
1. Using pre & post scripts
You can run the second script directly and define the third one as a post script.
{
"scripts": {
"one": "npm run two --",
"postone": "npm run three",
"two": "gulp build",
"three": "another random command"
}
2. Using npm-run-all
Pass argument placeholders to individual scripts.
"scripts": {
"one": "run-s 'two -- {#}' three --",
"postone": "npm run three",
"two": "gulp build",
"three": "another random command"
}
3. Using concurrently
Change the first script to run everything with concurrently, instructing that the second script should get pass through arguments.
{
"scripts": {
"one": "concurrently -P 'npm run two -- {#} && npm run three' --",
"two": "gulp build",
"three": "another random command"
}
}
Since we are passing only one command to concurrently, there is no concurrency.