`package.json` sequential start script only execute one of the script - node.js

I have the following scripts property in package.json:
"scripts": {
"start": "webpack -w && nodemon server.js",
"watch:server": "nodemon server.js",
"watch:build": "webpack -w",
},
When I run npm run start it only starts the webpack -w command but not the nodemon server.js command. I thought having && will run the scripts sequentially but it is definitely not the case for me. I have seen many people chaining execution with && and it works fine.
Why is mine not working?

If webpack -w is not finishing successfully, your second command nodemon server.js won't run. Using a single & should allow the second one to run even if the first one fails.

Related

package.json: what is the difference between & and &&?

Title pretty much says it all, but I'd also like to know if these commands work or behave differently depending upon the OS.
example1:
"scripts": {
"build": "babel -d serverbuild ./server",
"exe": "node ./serverbuild/index.js",
"start": "npm run build && npm run exe"
}
example2:
"scripts": {
"build": "babel -d serverbuild ./server",
"exe": "node ./serverbuild/index.js",
"start": "npm run build & npm run exe"
}
Given these examples portions of a package.json, what would be the difference between npm run start?
When using &&, the first command will run, and if it does not error, the second command will run. It's like a logical AND.
Using &, however, will run a command in the background. So in your second package.json, npm run build will start running in the background and then npm run exe will run as well regardless of what happens to the first command.

Syntax (windows and linux) for multiple commands using Node cross-env

I'm taking a look at a project that has this line in it's package.json to run Karma tests
"scripts": {
"test": "NODE_ENV=test karma start karma.conf.js",
This doesn't work in Windows when I try "npm test".
I gather this is because this is a *nix syntax. And, in fact, if I change it to
"scripts": {
"test": "set NODE_ENV=test && karma start karma.conf.js",
the tests start when I run npm test.
Looking around, the optimal solution appears to be to use the cross-env package and rewrite it like
"scripts": {
"test": "cross-env NODE_ENV=test && karma start karma.conf.js",
So I get the cross-env will take care of the "set NODE_ENV" part to work on multiple OSes, but it's the "&&" part I'm questioning.
Do I leave the "&&" between the commands when using cross-env? Will that work in windows and linux?
Your cross-env example won't work, it should just be:
"scripts": {
"test": "cross-env NODE_ENV=test karma start karma.conf.js",
without the &&. The command to run comes immediately after you're done setting the variables.
I found your question by searching for how to set multiple variables with cross-env and also how to run multiple scripts/commands with cross-env. So to address the "multiple commands" part of your question:
Given these 2 test scripts:
a.js
console.log('a', process.env.TEST_VAR, process.env.TEST_VAR_2);
b.js
console.log('b', process.env.TEST_VAR, process.env.TEST_VAR_2);
You'll find if you have an && in the script in your package.json the subsequent scripts don't receive the variables. For example:
"scripts": {
"check": "cross-env TEST_VAR=hello TEST_VAR_2=world node a.js && node b.js",
Running npm run check gives:
a hello world
b undefined undefined
You can solve this by having an extra script which runs the multiple commands and running that with cross-env:
"scripts": {
"check": "cross-env TEST_VAR=hello TEST_VAR_2=world npm run check-real",
"check-real": "node a.js && node b.js",
Now npm run check gives:
a hello world
b hello world

nodemon ''mocha' is not recognized as an internal or external command, operable program or batch file

Running a test for a nodejs project on windows 10
with the line in package.json as:
"test": "nodemon --exec 'mocha -R min'"
I get:
> nodemon --exec 'mocha -R min'
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `'mocha -R min'`
''mocha' is not recognized as an internal or external command,
operable program or batch file.
[nodemon] app crashed - waiting for file changes before starting...
rs
[nodemon] starting `'mocha -R min'`
''mocha' is not recognized as an internal or external command,
operable program or batch file.
[nodemon] app crashed - waiting for file changes before starting...
That worked fine with the line:
"test": "nodemon --exec \"mocha -R min\""
in package.json
install mocha globally then it will work
npm install -g mocha --save-dev
If you are using windows OS the do not use the single quotes
"test": "nodemon --exec 'mocha -R min'"
Use this
"test": "nodemon --exec mocha -R min"
Visit: www.mycodingx.com for more
"test": "mocha **/*.test.js",
"test-watch": "nodemon --exec \"npm test\""
For Run
npm run test-watch
Also, check your NODE_ENV=development if you are on Windows and are using git-bash. For some reason, it defaults to production.
$ echo $NODE_ENV
With the --production flag (or when the NODE_ENV environment variable is set to production), npm will not install modules listed in "devDependencies"
You can verify this by checking your node_modules/ folder and see if mocha was installed. If not:
$ npm install --only=dev
also:
$ NODE_ENV=development
$ npm i -D mocha
would do the trick.
I am no Windows kernel or any .. expert. In my case the test script kept erroring out with the message npm is not recognized as an Internal or External command.
a) When I had it as
"test": "mocha **/*.test.js",
"test-watch": "nodemon --exec 'npm test'"
It ran a few times and stopped and the error started occurring so when I switched to
"test": "mocha **/*.test.js",
"test-watch": "nodemon --exec \"npm test\""
Still I kept getting the same error of npm not recognized...
And no matter how many times I issued Ctrl c, the nodemon wouldn't stop.
I did take the steps of restarting my laptop, uninstalled and re-installed nodeJs, updated the PATH variable in Control Panel - User Accounts - Environment variables all amounting to no end in sight.
This leads me to believe that somewhere or somehow, either nodemon or mocha not sure, what is hanging, so even after I had modified to escape and use double quotes as in
"test": "mocha **/*.test.js",
"test-watch": "nodemon --exec \"npm test\""
I still kept getting the same error.
b) So then I changed the name of the key from test-watch to test-new
"test": "mocha **/*.test.js",
"test-new": "nodemon --exec \"npm test\""
and ran
npm run test-new
and every tests runs fine.
Go figure...
So I think I will stick to keeping unique test script names between different projects. I have no other explanation.... Anyone can shed light on this? Please do so...
for executing npm test command install mocha globally with -g
command: npm install mocha -g
I had the same issue and worked after installing mocha globally.
provided you have all dependency ready in your project
Inside the package.json, you need to add a new script right after the "test" script. We can create a custom script and named it as "test-watch" and the value of the "test-watch"is "nodemon --exec \"npm test\""(i.e "test-watch": "nodemon --exec \"npm test\"") After this step we can use npm run test-watch command in terminal.
An alternative approach is to add the mocha path to the environment variables then restart the bash
On your editor, navigate to the bin folder of mocha and add both paths to your system environments.
All script options that have been illustrated work with this approach
"scripts": {
"test": "nodemon --exec \"mocha -R min\""
}
or
"scripts": {
"test": "nodemon --exec 'mocha -R min'"
}
or
"scripts": {
"test": "nodemon --exec mocha -R min"
}
in the package.json file are correct dependencies definition
I hope this helps fix the issue.
Use "npm run test" and the command should be "nodemon --exec "mocha -R min"". For me, it worked when the previous command is used instead of the npm test & "nodemon --exec 'mocha -R min'"

How do I execute typescript watch and running server at the same time?

I was developing my project in nodejs. I found if I need to code and test api, I will run two console, one is to execute typescript watch, another is to execute server.
I think it's so troublesome. I find other developers on github have written scripts in package.json. It's easy to call any commands. It attracts how to write the scripts and simply my development workflow.
In short, the comand of typescript watch is tsc -w and the comand of running server is node app.js. My idea is merge the commands as tsc -w & node app.js but I can't work the two commands at the same time. How do I do? Thanks.
My idea is merge the commands as tsc -w & node app.js but I can't work the two commands at the same time. How do I do
You have a few options. Simplest is to use ts-node-dev : https://github.com/whitecolor/ts-node-dev
Option 1
Step 1
install concurrently, use npm, pnpm or yarn
pnpm i concurrently -D
Step 2
create a script with this command
"scripts": {
"run": "tsc && concurrently \"tsc -w\" \"nodemon dist/app.js\"",
}
Option 2
without install anything (mac or Linux)
"scripts": {
"run": "tsc -w & nodemon dist/app.js",
}
run tsc first so that your directory has something at the time of running node
And with that you will have running your Typescript application 🚀
Another option can be to use nodemon:
tsc -w & nodemon app.js
Since Typescript 3.4 the compilation is faster because you can use the incremental compiler option and they keep improving (including interesting changes for large projects in 3.8).
Update:
I also moved to use concurrently as HerberthObregon says in his answer
TLDR, If you like nodemon this is a straight forward way to get file watch, compilation and execution:
nodemon --ext ts --exec 'tsc && node dist/index.js'
Optionally replace tsc with babel for faster compilation.
Here is a more complete example, in package.json (with source maps):
"scripts": {
"develop": "nodemon --ext ts --exec 'yarn build --incremental && yarn serve'",
"build": "tsc",
"serve": "node --require source-map-support/register dist/index.js",
...
},
Install source-map-support as a dependency if you want, ahem... source map support! Otherwise, remove --require source-map-support/register from the serve script above.
tsconfig.json
{
"compilerOptions": {
...
"sourceMap": true,
"outDir": "dist",
}
}
Building on herberthObregon's answer
Step 1: Install packages
npm install concurrently typescript nodemon --save-dev
Step 2: Create these scripts in package.json
"scripts": {
"build": "tsc",
"build:watch": "tsc -w",
"dev": "npm run build && concurrently \"npm run build:watch\" \"npm run serve:watch\"",
"serve": "node dist/index.js",
"serve:watch": "nodemon dist/index.js"
},
build runs a standard typescript build
build:watch runs typescript build in watch mode
serve serves up your node project (assuming your tsconfig outputs to dest/index/js)
serve:watch uses nodemon to restart the node server whenever the js output changes
dev puts them all together
Just going to throw my hat in here, here's a solution using ts-node-dev and concurrently, similar to the one provided by #HerberthObregon but using ts-node-dev instead of nodemon:
"scripts": {
"start": "npm run build && concurrently \"npm run build:watch\" \"npm run dev\"",
"dev": "tsnd --respawn src/main.ts",
"build": "tsc -p tsconfig.release.json",
"build:watch": "tsc -w -p tsconfig.release.json"
}
Bonus: If you need help with your figuring out tscand your tsconfig.json, I use the sensible defaults from this node typescript starter.
Here's a solution that works for me
1. Install ts-node and nodemon as dev dependencies
2. Create a script : "dev" : "nodemon app.ts"

Only the first task run while chaining tasks on package.json

I have these scripts as part of my package.json:
"scripts": {
"clean": "rm -rf lib",
"watch-js": "./node_modules/.bin/babel src -d lib --experimental -w",
"dev-server": "node lib/server/webpack",
"server": "nodemon lib/server/server",
"start": "npm run watch-js & npm run dev-server & npm run server",
"build": "npm run clean && ./node_modules/.bin/babel src -d lib --experimental"
}
When running 'npm run start', only the first task in the chain is running (npm run watch-js).
When running each one of these chained tasks simultaneously they work.
Also when swapping places between the tasks, always the first task is the only one to run.
How can i make all of the chained tasks under "start" to run?

Resources