Start node.js with bash script? - node.js

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

Related

package.json scripts - I want to have it wait to run other parts of script until one part is executed fully, how to do that?

Currently I have this script in my package.json file:
"npm run build:main:dev && concurrently -k -n Main,Rend -c yellow,cyan \"electron --inspect=5858 --ignore-certificate-errors ./build --watch\" \"npm run build:renderer:watch\""
I would like to add something to it, so that it will completely finish running npm run build:main:dev before it goes on to run the rest of the script. How can I do that?

How to call Terminal Commands in a TEXT File?

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!

How to run few npm scripts in one-line shell command?

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

How to run npm test after npm start in bash script

In my docker file, I want to run test script inside it after app is up in Docker dev version. Problem is when I created a bash script and add like that, test script is not working.
Here is my package json file:
"scripts": {
"build": "./node_modules/babel-cli/bin/babel.js src --out-dir lib --copy-files lib --plugins transform-react-jsx --presets es2015",
"bundle": "./node_modules/browserify/bin/cmd.js lib/client.js -o public/js/bundle.js",
"start": "npm run build && npm run bundle && node lib/server.js",
"test": "mocha ./src/*.test.js"
},
and here is bash script file
#!/bin/bash
npm start
npm run start
npm test
npm run test
Please let me know how to fix it, thanks much.
#!/bin/bash
npm run build
npm run bundle
forever start ./lib/server.js
npm test
I've found solution that I need to install forever or pm2 first.

Run NPM scripts synchronously

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');

Resources