How to start multiple node scripts with PM2 - javascript

I want to run these pm2 tasks:
"pm2-frontend": "pm2 start --name frontend npm -- start",
"pm2-storybook": "pm2 start --name storybook npm -- storybook"
Which should run these two package.json scripts:
"storybook": "start-storybook -p 6006",
"start": "next start"
But, this only seems to start the start script. Is there a way to target scripts with another name than start?

Use the word run before you run scripts other than start.
Start is a default script name so you can run it with npm start but storybook is not a default script so you gotta use npm run storybook

Related

npm parallel execution of commands not working

I have a package.json file at the root of my project folder which contains client and server folders.
in the root package.json I have the following scripts:
"scripts": {
"server": "npm run watch --prefix server",
"client": "npm start --prefix client",
"watch": "npm run server & npm run client"
}
but when i try to run npm run watch only the server script runs, what I don't understand is if I paste the contents of the watch script into the terminal it works just fine.
npm run <script> uses cmd.exe by default if you are on windows, and cmd does not support & to run commands in parallel, so it is better to use a package like npm-run-all.

How can I run a yarn app/How to run a yarn dev server?

I've always used just npm and never yarn/webpack explicitly. I need to run the code from this repo:
https://github.com/looker-open-source/custom_visualizations_v2
Like a dev server or something to ensure it's serving the files properly but I don't see a "run" like npm run start. Does this just not exist with yarn? It feels like this code should work as is and I shouldn't have to add anything.
EDIT: I've now tried yarn run watch but it just seems to build the code again and not actually host anywhere
npm run somecommand just looks up in the "scripts" field of package.json for the key
somecommand and executes the value in the terminal.
So npm run start basically runs the start script from package.json
The same thing is done using yarn via simply yarn start
In the linked repo, there isn't a start script in the package.json, rather a watch script, so you should be able to run it with the below steps:
yarn to install dependencies after cloning the repo to local (similar to npm install)
yarn watch to start the webpack server (analogous to npm run watch)
Edit:
Turns out the watch command is just setting up webpack to watch for changes and recompile the project every time there is a change.
To run a development server, you will need to add another script preferably with name start and use webpack-dev-server
So the package.json has entries like:
...
"watch": "webpack --config webpack.config.js --watch --progress",
"start": "webpack-dev-server --config webpack.config.js",
...
Then running yarn start should open a dev server at localhost:8080

How to run multiple NPM scripts in sequence with watch

I am working on angular 7 application. Below are the scripts in my package.json file to build the application
"scripts": {
"build:all": "npm run build:dev & npm run postbuild",
"build:dev": "ng build --extract-css --watch",
"postbuild": "node fileMover.js",
}
I want to run both build:dev and postbuild commands in sequence.
so first command builds the application by updating the dist folder with bundle files. To move the bundle files to separate folders, we have created a fileMover.js node script.
without --watch, build:all script works fine but due to this, we need to manually run the command everytime we make any changes & save through visual studio or other tool. With watch, it never runs the second "postbuild" command.
Is there any alternate to run both scripts (fist with watch) in sequence?
Thanks :)
You could change the & to && as a single & is for running in parallel and double is for one after-the-other, I believe.
scripts": {
"build:all": "npm run build:dev && npm run postbuild",
"build:dev": "ng build --extract-css --watch",
"postbuild": "node fileMover.js",
}
You could also use the 'pre' and 'post' affixes:
scripts": {
"build:dev": "ng build --extract-css --watch",
"build:all": "npm run build:dev",
"postbuild:all": "node fileMover.js",
}

How to Start PM2 Node JS with Babel and presets

I'm having the following code in my package.json to start the script while developing:
....
"scripts": {
"start": "nodemon src/index.js --exec babel-node --presets es2015,stage-2"
},
....
Now I want to deploy it to production. When I run npm start everything works fine. However, it will shut down when I close the terminal. So how can I use it with PM2?
This is what I've tried:
pm2 start src/index.js -x babel-node -p es2015,stage-2
You can actually use npm start if you like:
$ pm2 start npm -- start
That said, for production deployment I would strongly recommend a) using a config file for your pm2 startup stuff (so you can bundle environment variables and such) and b) precompiling your assets as a build step rather than on startup.

Why is tsc called twice in this npm start script?

The package.json example at this link includes the following start commands:
"start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
What explicitly does the above command do?
I think that the "concurrently \"npm run tsc:w\" \"npm run lite\" " means to start both tsc and lite-server simultaneously, and to also place a watch on the tsc so that changes are reloaded into lite-server immediately. Is this correct? And also, why call tsc twice? What is an explicit explanation of the entire line of code including all of its constituent parts put together?
You can break it down the command into parts (with quotes removed):
tsc
concurrently
npm run tsc:w
npm run lite
The first part calls the TypeScript compiler CLI and compiles your TypeScript files.
Next, there is an && which means "cmd1 then/and cmd2". The next section:
concurrently npm run tsc:w npm run lite
Uses the concurrently package CLI to run the given commands, which are npm run tsc:w and npm run lite. The part:
npm run tsc:w
This runs the script in your package.json:
"tsc:w": "tsc -w"
Then npm run lite runs the corresponding script in package.json:
"lite": "lite-server"
So you're technically calling tsc twice but tsc:w starts watching your TypeScript files. Using -w does not do an initial build, so the first tsc is needed to build your files initially, then -w watches your files and rebuilds subsequent changed files. The concurrent script then runs the watch script and the server.

Categories

Resources