This is the portion from package.json :
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon --watch app index.js"
},
All my code except the index.js, package.json and node_modules resides in a sub folder called app.
When I run using npm run dev , nodemon watches the changes in app folder and restarts if there's any changes. But won't restart if I make any changes in index.js (entry point)
My folder structure:
|-- app/
|-- node_modules/
|index.js <--- nodemon not watching this file
|package.json
|package-lock.json
Why is it so?
EDIT:
Here's the solution (from #Pedro Filipe):
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon index.js"
},
If you're importing files into index.js, I think you just need to do nodemon index.js in order to have the files that interest you watched. I suspect that when you pass the flag --watch [folder_name] it basically just ignores the filename you pass afterwards.
Nodemon detects changes into the files and restarts it automatically.
Currently, you are saying node to run the index.js file which requires restart whenever you want to reflect your changes.
All you need to do is change the "start" command.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon index.js"
}
(Previously commented this answer)
Nodemon watches the entry-point to your project.
Which, In almost all the cases of node project is a single file which eventually imports other files and so on and so forth.
(Assuming that your entry-point is index.js i.e. you're importing your other files in there.) You can simply use the nodemon index.js as the script for your dev
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon index.js"
},
Nodemon detects changes into the files and restarts it automatically.
Currently, you are saying node to run the index.js file which requires restart whenever you want to reflect your changes.
All you need to do is change the "start" command.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon index.js"
},
Related
I'm deploying Nodejs application to Droplet of Digitalocean.When I start it normal(npm start), it work perfectly.But when i start with pm2 , i get this error in pm2 logs.
Error during Data Source initialization error: password authentication failed for user "root"
this is script in my package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "tsc -w",
"server": "nodemon dist/index.js",
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js -d ./dist/data-source.js",
"build": "rm -rf dist && tsc",
"start": "NODE_ENV=production node dist/index.js"
},
I dont use root as owner rule when create database on Droplet, but i dont know why it happen, how can i fix it?
So I was having this issue of node not restarting the server when there changes made on the server.js file. I fixed it by editing the package.json file. However, once fixed, the npm start command starts the server.js file: [nodemon] starting `node server.js
How do I start my react app then? npm start was the command I used before
Most probably your set your package.json file to
"scripts": {
"start": "nodemon server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
Change your server package.json to something like this:
"scripts": {
"server": "nodemon server.js",
"test": "echo \"Error: no test specified\" && exit 1"
then run the command
npm run server --this will start your server.js
Nodemon has nothing to do with the client-side(React app). So, before running npm-start:
Make sure you are in the correct directory cd [directory-name]
It's better to separate your front-end and back-end in a separate folder
I have client and server in a separate folder. I think there is an error in client Script. can anyone help me out with the solution. I want to run both client and server simultaneously. Thanks
my folder structure
----client
----server
package.json inside server folder
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"server": "nodemon server.js",
**"client": "npm start --prefix client",**
"dev": "concurrently \"npm run server\" \"npm run client\""
}
As you have separate package.json files for the server and client, please try this solution
package.json inside the server folder
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"server": "nodemon server.js",
**"client": "npm start --prefix client",**
"dev": "concurrently \"npm run server\" \"cd ./../client && npm run client\""
}
Also you can try this one
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"server": "nodemon server.js",
**"client": "npm start --prefix client",**
"dev": "npm run server & (cd ./../client && npm run client)"
}
I'm building a project that has a backend, and a frontend.
I had the idea that'd be cool to have both folders in the same directory with another npm script that runs them.
The problem is that I'm unable to set the port in one of the two packages, that being the frontend.
In my main package.json I have this:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start-app": "cross-env SERVER_PORT=8080 npm start --prefix ./frontend",
"start-server": "cross-env SERVER_PORT=8080 npm start --prefix ./backend"
},
And it works for the backend, but doesn't for the frontend.
The frontend has the base configuration.
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
The project runs, but when I look for the process.env, there's no trace of the SERVER_PORT in the FrontEnd but it exists in the BackEnd.
(Backend scripts are the default for a new npm package).
"scripts": {
"start": "node main.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
CRA uses PORT to define the port variable. Run it like:
"start": “PORT=8000 react-scripts start"
Additionally since CRA is essentially a static site it doesn't have in itself a concept of env variables - these are NodeJS specific. A few such as PORT and NDOE_ENV are defined for you and used in the dev server. If you wanted to use custom env vars within your React components you can make them available by defining the with the prefix: REACT_APP_ such as REACT_APP_SITENAME for example.
Check out the docs here: https://create-react-app.dev/docs/adding-custom-environment-variables/
I tried implementing custom nodemon configuration in package.json as shown below:
"nodemonConfig": {
"watch": ["server", "bin/www"],
"ext": "ts",
"ignore": ["*.test.ts"],
"delay": "3000",
"execMap": {
"ts": "ts-node"
}
}
Yet, it didn't work. Nodemon doesn't restart when ./bin/www is edited, nor does ignoring the files that restart the server works.
Can anyone suggest me the correct nodemonConfig?
I think these settings only work when npm is starting nodemon, something like
// package.json
"scripts": {
"start:dev": "DEBUG=app:* nodemon app.js"
}
then use
$> npm run start:dev
package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon index.js"
},
Terminal
npm run dev