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

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?

Related

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

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.

How to npm install client and server project dependencies with single command?

I have a backend and frontend parts of the project in single root folder:
./app/ - backend files and folders
./app/forntend/ - front end files and folders
in my app folder I have package.json with scripts and backend dependencies, installation script here is: "install": "npm i && cd ./frontend && npm i", I also have package.json in frontend with its own denendencies
when I run installation script it enters into infinite loop of installs, which I have to terminate manually.
Is there way to have a single installation script in package.json?
You can solve this problem by using concurrently npm module.
./app/ - backend files and folders
./app/forntend/ - front end files and folders
With concurrently installed in your root folder i.e. ./app/->backend, you can run multiple custom npm scripts. For example: you can create 2 separate scripts that are installing the dependencies (client-dependencies and server-dependencies) and then create install-all-deps script that will run both scripts one after another and install all deps in both directories.
{
"scripts": {
"server-dependencies": "npm install",
"client-dependencies": "npm install --prefix forntend",
"install-all-deps": "concurrently \"npm run server-dependencies\" \"npm run client-dependencies\""
}
}
Link for Concurrently :- https://www.npmjs.com/package/concurrently
Hope this helps.
If you are installing concurrently part of server app then you will get an error concurrently is not installed. For that we have install concurrently first in following way
"scripts": {
"install-server": "npm install",
"install-client": "npm install --prefix forntend",
"install": "npm install concurrently --save & concurrently \"npm run install-server\" \"npm run install-client\"",
"start": "concurrently \"nodemon node_server/server.js\" \"npm run build\"",
"test": "echo \"Error: no test specified\" && exit 1"
},
where
Use && (double ampersand) for sequential execution.
Use & (single ampersand) for parallel execution.

Using Nodemon or something similar to listen for changes, first build, then start?

Using Nodemon or something similar to listen for changes, first build, then start? Is it possible?
"scripts": {
"build": "npm run build:dll && webpack --progress",
"start": "node app.js",
}
Make sure nodemon is installed (npm install -g nodemon or npm install --save-dev nodemon) and then just change your package.json to this:
"scripts": {
"build": "babel lib -d build --copy-files",
"start": "nodemon build/index.js"
}
EDIT:
Add a nodemon.json on the root of your project, in there insert your build script in the "events.restart" section as documented here: https://github.com/remy/nodemon/blob/master/doc/sample-nodemon.md
"events": {
"restart": "your build script here"
}
And finally run with "npm run start". This run your app with nodemon and nodemon's configuration will execute your build very time you change your code (on restart)

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"

Npm scripts doesn't work the way I want

see below:
scripts": {
"build": "node_modules/.bin/babel sercer/src --out-dir server/dist ",
"build:watch": "node_modules/.bin/babel server/src --out-dir server/dist --watch",
"start:server": "node ./node_modules/nodemon/bin/nodemon.js ./server/dist/app.js",
"dev" : "(npm run build:watch) && (npm run start:server)"
}
you know, both of them can work well when I run npm run xxx , but when i conbian them like npm run dev does,the last one will not taking effect.what wrong with my script?
You could try
"dev" : "npm run build:watch && npm run start:server"
you can use the post- and pre- scripts that will be called before and after that script.
eg :
"build": "npm run build:css && npm run build:js",
"prebuild:js": "npm run lint"
In the above example build will execute both build:css and build:js - but not before running the lint task.

Resources