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
When working locally on a NodeJS project, nodemon is required in order to make the coding easier.
I frequently see the cases when it's installed as a dev-dependency only, so I wonder: what is the correct approach when it comes to deployment? Should we include it as a dev-dependency only, or should we include it into the server as well?
In this project I have, I see nodemon installed as regular dependency and then in the package.json configs:
"scripts": {
"start": "nodemon src/app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
But I am thinking to install it as a dev-dependency only and then to rework the config like:
"scripts": {
"start-prod": "node src/app.js",
"start-dev": "nodemon src/app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
So I wonder if this will be a correct approach?
I don't see a reason why on the server I would watch the file changes with nodemon, so I wonder if I got it right? In case sometimes it is needed, what are the possible cases when that will be required?
Short answer: You don't require nodemon in production.
According to nodemon on npm:
nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.
It is a tool to help in development, mainly restart the app server on file changes. You can add it to dev dependencies if you want to run nodemon via scripts. Otherwise you can install it globally.
npm install -g nodemon # or using yarn: yarn global add nodemon
And nodemon will be installed globally to your system path.
You can also install nodemon as a development dependency:
npm install --save-dev nodemon # or using yarn: yarn add nodemon -D
Edit:
If you want to keep your app alive even in case of any crash, you should consider using pm2.
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
I am working on a project. I have worked with nodemon that is installed globally for development.
I edited my package.json file to add a script to automatically run a nodemon script - as shown below:
"scripts": {
"start": "node ./bin/www",
"dev": "nodemon -e js,pug"
}
Now when another developer runs:
npm start dev
they will surely get a error if they have not installed the nodemon module.
I know that the solution is to install nodemon locally as a development dependency.
Is it possible to work around this problem without installing it locally?
Can I install nodemon both locally and globally at the same time?
simply install it globally and you can use it in any of your project
command :
npm i -g nodemon
now you don't need to install it locally at all to make it work on your project.
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