To start my project, I have 4 terminal commands that need executing in order, one after another and in different terminal windows:
npm install
gulp
php -S localhost:8001
browser-sync start --proxy "localhost:8001" --files "**/*"
To speed things up, I want to put theses in the package.json and execute them with a single command like so:
scripts: {
"start": "npm install && gulp && php -S localhost:8001 && browser-sync start --proxy 'localhost:8001' --files '**/*'"
}
But won't this run them asynchronously and in the same shell?
You must try exec-sync and provide the scripts command one by one.
var execSync = require('exec-sync');
execSync('npm install');
execSync('gulp');
execSync('php -S localhost:8001');
Related
What to do when I have to start a node.js application with a script on the raspberry pi?
Normally I go to the command line move to the map with "cd projectMap" and do this command "npm projectName"
I thought this would work but it does not.
#!/bin/bash
cd projectMap
npm projectName
to run a nodejs script you generally run
node index.js
Replace index.js with the name of the file you want to run.
To run it with npm you have to add it as a script in the package.json like so
"scripts": {
"start": "node index.js"
}
And to run it:
npm start
Create a file:
sudo touch script.sh
Give permission:
sudo chmod 400 script.sh
Inside your script.sh
#!/bin/bash
cd project
npm -i
node -v // not necessary just to check the node version .
node file.js
run script.sh
./script.sh
I really need help on this, couldn't find much examples on google.
For example:
I have this web project. Inside this project there is, app.js, views/home.ejs, some npm packages and etc...
This project, we are not allowed to type "npm install" "npm start" to run our project or we get a zero. The teacher will only type 1 line in terminal which is "run app.py" for python or "run js app" to run our code, will not install packages on localhost.
Wants us to make a textfile to automatically install all packages in the background and run the application automatically? How would I do that?
For example in text file:
inside TXT.FILE {
#1 run "npm install express" in terminal
#2 run "npm install body-parser" in terminal
#3 run "node app.js" in terminal
#4 also run "ls"
}
Basically just call terminal commands in a text file. A text file that will automatically execute them in order.
You have two options.
Add this into your package.json file
...
"scripts": {
...
"start": "npm install express && npm install body-parser && node app.js && ls"
},
Now you can use npm start to run all these commands at one go.
Add a bash script in your project directory.
The file should be named your-script-name.sh.
Inside add
#!/bin/bash
npm install express && npm install body-parser && node app.js && ls
You can run the script using ./your-script-name.sh in your terminal.
"scripts": {
...
"start": "npm install express && npm install body-parser && node app.js && ls"
},
Yes this works very well, tested it! Thank you.
This calls terminal commands under 1 line of code!
I have three scripts in package.json:
Watch server TypeScript
Nodemon
Webpack
"scripts": {
"watch-server": "tsc --watch --project ./server/tsconfig.json",
"watch-node": "nodemon --watch ./server/build/ --watch ./server/templates -e js,json,pug",
"watch-client": "webpack --config ./webpack/webpack.dev.conf.js --watch"
}
Everytime I start my computer and open VS Code I need to open three separate PowerShell terminals and type in those commands one-by-one. Is there any way to launch these three separate terminals with their own commands in one shell command? Maybe via tasks.json?
On linux or any bash terminal, you can use && to combine multiple commands, i
You can do as
npm run watch-server && npm run watch-node && npm run watch-client
A quick google search for powershell suggested using semicolon
so on powershell you can do something like below if using && does not work
npm run watch-server;npm run watch-node ; npm run watch-client
Also keep in mind, you can additionally add fourth command in your npm scripts in package.json where you can use one of these combined commands which works for you, like
start-all: npm run watch-server && npm run watch-node && npm run watch-client
and then run
npm run start-all
Hi is there a way i can keep webpack devserver running even after closing terminal.
"scripts": {
"dev-server": "npm run templates && webpack-dev-server -d --https --port 28443",
}
when i run npm run dev-server it starts but after closing terminal webpack devserver also closes is there any way to keep it running with pm2 or any other method.
use nohup
So if the script command is "dev-server" (according to your snippet), then go to your project root directory (where the package.json file is, which has your "scripts" section):
If on unix environment, just run nohup npm run dev-server &
If on windows, install git bash - it's sort of a mini unix environment for windows. And then run the nohup npm run dev-server &
This will start the webpack dev server and keep it running on the background
For me, my script section is
"scripts": {
"dev": "webpack-dev-server --open"
}
and the above command that I mentioned works fine
nohup did somehow not work for me, but i was able to make it work with forever.
sudo npm install -g forever
forever start -c "webpack serve" ./
I have multiple scripts in package.json, most of them call some .js e.g. "copyResources" bellow. If I will run "npm run -s copyResources" in shell, it will execute silently. So far so good. But I would like to run "npm start" which will execute copyResource silently, example bellow, but this is not working (it's not silent). :(
Later I will have more scripts in "start" and I want to run some of them silently and some of them not. Thats the case why I can't just do npm start -s ...
"scripts": {
"copyResources": "echo 'Copy resources =>' && node ./bin/copyResources.js",
"start": "npm run -s copyResources"
}
Thank you very much!