I installed pm2 with NPM without the global flag. Then I started the app with "npm start" that declared on package.json as
"start": "pm2 start index.js"
How can I stop it?
You can access locally-installed pm2 with npx:
$ npx pm2 list
$ npx pm2 stop 0 # first app will have ID=0
But in general, I would manage it by attaching a name to the app. Let's edit your package.json:
"start": "pm2 start index.js --name my-app"
"stop": "pm2 stop my-app",
"logs": "pm2 logs my-app"
And then:
$ npm run start
$ npm run logs
$ npm run stop
Oh, was easy as installing it globally
npm install pm2 -g
and then kill the process. I thought it will be a "different" thing
Related
I have the following line in my package.json
"scripts": {
"start": "cross-env NODE_ENV=development node index.js"
}
I can see that "yarn start" command is running fine, but when I run
"cross-env NODE_ENV=development node index.js" command directly in the terminal, I am getting the following error:
zsh: command not found: cross-env
If cross-env is not registered in the terminal, how does "yarn start" command works?
https://docs.npmjs.com/cli/v7/configuring-npm/folders#executables
When in local mode, executables are linked into ./node_modules/.bin so that they can be made available to scripts run through npm. (For example, so that a test runner will be in the path when you run npm test.)
It's simply a feature to make things easier. It also means if you're working a project with multiple people, you only have to npm install --save a module--you don't have to worry about everyone in your project manually installing it globally. If you wish to run it yourself on the command line, you can either:
Install the module globally
Manually type in the command line ./node_modules/.bin/cross-env
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.
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 am running my nodejs app by npm start
I just installed nodemon by
sudo npm install -g nodemon so that i can get my server restarted when i save changes to files.
But when i try to start the server, something like this
nodemon ./app.js localhost 3000 or nodemon start localhost 3000
I get this as output
LM-SJC-00871929:webapp gdeep$ nodemon ./app.js localhost 3000
28 May 23:34:30 - [nodemon] v1.1.1
28 May 23:34:30 - [nodemon] to restart at any time, enter `rs`
28 May 23:34:30 - [nodemon] watching: *.*
28 May 23:34:30 - [nodemon] starting `node ./app.js localhost 3000`
but when i go to my webpage, i get
Oops! Google Chrome could not connect to localhost:3000. What am i doing wrong?
App.js here http://collabedit.com/t35dy
You're running express 4, which has the app.listen call in a different file than app.js. The command you're looking for is nodemon bin/www (localhost and 3000 are not needed in this scenario).
In fact, you can even run nodemon with no args, and it'll read what command needs to be run from scripts.start in package.json (which express generates automatically).
Here's what I did to make nodemon update correctly:
nodemon index.js -L
The -L flag stands for legacyWatch, here's an explanation from the official doc:
In some networked environments (such as a container running nodemon reading across a mounted drive), you will need to use the legacyWatch: true which enables Chokidar's polling.
https://www.npmjs.com/package/nodemon#application-isnt-restarting
In my case, I had to install nodemon globally. Use this command to do so..
npm install -g nodemon
If you're using Linux you may need to prefix the command with the sudo keyword for administration access..
sudo npm install -g nodemon
try running nodemon ./app.js 3000 or nodemon start 3000
For Express.js 4,
use nodemon
or nodemon bin/www
Add following code in your code
app.js
app.listen(3000, function(){
console.log("info",'Server is running at port : ' + 3000);
});
package.json
nodemon app.js
Then run npm start from the command line.
npm i nodemon
Edit your Package.json file:- set start to nodemon:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
}
Run npm start to try the server
If you are using express4, the easiest way is to navigate to package.json and change
"scripts": {
"start": "node ./bin/www"
}
to
"scripts": {
"start": "nodemon ./bin/www"
}
For Express 4; Just run
nodemon
command (with out any args) on the directory; this works for me.
thanks
you need to type this after to enter in the folder application with
cd your_project_folder
sudo nodemon bin/www
Certain child processes related to your parent process may not be closed. Try to kill all the child processes.
Ref: https://github.com/remy/pstree
Use single quotation for multi-value args like `--exec'.
e.g. I changed "nodemon --exec yarn build-langs" to "nodemon --exec 'yarn build-langs'" and worked.
"scripts": {
"start": "nodemon app.js"
},
check it, if it's true or not
Try this:
First install nodemon by npm install nodemon or any command from the official website (https://www.npmjs.com/package/nodemon)
Then start your nodemon by npx nodemon filename.js
or if you have your package.json with you and the entry point is the file you would like to open then you can also go with the command npx nodemon
Check this once
Hope it works!!
Happy Coding 😀
You can directly use the command nodemon start to start your server.
Try this command on terminal if you are using Nodejs & TypeScript
npm i --dev nodemon ts-node
You might also run into the issue of having an empty .nodemonignore.
try
npm install --save-dev nodemon
and then
in
package.json file
keep like this
"scripts": {
"start": "nodemon",
"test": "echo \"Error: no test specified\" && exit 1"
},
instead of npx nodemon, which takes more time
First of all, uninstall nodemon:
npm uninstall nodemon
After uninstalling the nodemon then install nodemon globally using:
npm i -g nodemon
It will install nodemon globally then run the following command:
nodemon index.js or app.js
For someone who is coming from PHP background the process of killing node and starting it again after every code change, seems very tedious. Is there any flag when starting a script with node to automatically restart node when code change is saved?
A good option is Node-supervisor:
npm install supervisor -g
and after migrating to the root of your application use the following
supervisor app.js
You should look at something like nodemon.
Nodemon will watch the files in the directory in which nodemon was started, and if they change, it will automatically restart your node application.
Example:
nodemon ./server.js localhost 8080
or simply
nodemon server
forever module has a concept of multiple node.js servers, and can start, restart, stop and list currently running servers. It can also watch for changing files and restart node as needed.
Install it if you don't have it already:
npm install forever -g
After installing it, call the forever command: use the -w flag to watch file for changes:
forever -w ./my-script.js
In addition, you can watch directory and ignore patterns:
forever --watch --watchDirectory ./path/to/dir --watchIgnore *.log ./start/file
Various NPM packages are available to make this task easy.
For Development
nodemon: most popular and actively maintained
forever: second-most popular
node-dev: actively maintained (as of Oct 2020)
supervisor: no longer maintained
For Production (with extended functionality such as clustering, remote deploy etc.)
pm2: npm install -g pm2
Strong Loop Process Manager: npm install -g strongloop
Comparison between Forever, pm2 and StrongLoop can be found on StrongLoop's website.
You can also try nodemon
To Install Nodemon
npm install -g nodemon
To use Nodemon
Normally we start node program like:
node server.js
But here you have to do like:
nodemon server.js
node-dev
node-dev is great alternative to both nodemon and supervisor for developers who like to get growl (or libnotify) notifications on their desktop whenever the server restarts or when there is an error or change occur in file.
Installation:
npm install -g node-dev
Use node-dev, instead of node:
node-dev app.js
Notification on Changing file so server start automatically
console out put
Follow the steps:
npm install --save-dev nodemon
Add the following two lines to "script" section of package.json:
"start": "node ./bin/www",
"devstart": "nodemon ./bin/www"
as shown below:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node ./bin/www",
"devstart": "nodemon ./bin/www"
}
npm run devstart
https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/skeleton_website
I use runjs like:
runjs example.js
The package is called just run
npm install -g run
Nodejs supports watching mode since v18.11.0. To run it just pass --watch argument:
node --watch ./index.js
Note: this is an experimental feature.