I have this script in my package.json
"watch": "nodemon --watch components --watch helpers --watch redux --watch services --watch types --watch views --watch assets -e ts,scss,css --exec \"npm run build:localhost\"",
So my nodemon is watching my local folders, I just save a single file and this many processes are created.
I use this script to call my webpack build command upon saving a file. This webpack command builds a dist which is the server by a local nginx to localhost.
Is there a way to go over the problem?
Related
We are working on nodeJs/ExpressJs we have configured multiple .env files for development and production and pointing it to package.json for different execution process, we have naming conversation issues at scripts.
Whenever we run npm run prod it takes to preprod configuration. what could be the issues?
Update: we have figured that the suffix of the script key is the same in the next script, after update/rename preprod to preProd the both runs fine. but why?
Eg :
"scripts": {
"dev": "clear; env-cmd -f ./config/hostedDev.env nodemon --exec babel-node index.js",
"prod": "clear; env-cmd -f ./config/prod.env nodemon --exec babel-node index.js",
"preprod": "clear; env-cmd -f ./config/preprod.env nodemon --exec babel-node index.js"
},
Apparently the issue is with the word 'pre'.
If you would have noticed it runs both preprod and prod commands (pre running first).
If you change the script name to 'postprod' the postprod script will run later.
So, I guess npm uses 'pre' as to run before the 'prod' script and then running 'prod' script itself.
I'm rather new to this area, so please excuse if this question is completely basic. I've seen some tutorials use nodemon to watch files, and restart servers on Nodejs backend. I've seen others use webpack to watch files such as create react app on frontend. Can you actually use nodemon to watch files and refresh pages on the front end?
Yes I believe you can set it up with your package.json scripts!
For instance with a node server with a create-react-app within a client folder you could declare a start script along the lines of:
"start": "concurrently \"nodemon server.js\" \"cd client && nodemon start\""
Then when you run npm start this will run nodemon on both the server file and the client folder
Just be aware this assumes your server file is named server.js and your client files are in a folder named client and will require you to have the concurrently dependency installed.
You could use something like this one if you already have create react app within a client folder.
"server": "nodemon server.js",
"client": "cd client && yarn start",
"dev": "concurrently --kill-others-on-fail \"yarn server\" \"yarn client\""
Concurrently allows us to run both on one terminal as well as it allows us to pass --kill-others-on-fail which means that if one breaks (control + c on mac), the other one will also break
This is my nodemon.json
{
"watch": ["src/**/*.ts"],
"exec": "node out/index.js"
}
I run the nodemon by executing:
nodemon
In root nodejs directory
This is output:
% nodemon
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: src/**/*.ts
[nodemon] starting node out/index.js
Yay! Started app!
But when I edit any ts file in src nodemon doesn't restart the app.
UPDATE
Running nodemon --watch src/index.ts --exec 'node out/index.js'
Works and reloads the app on modifying index.ts
However, running with wildcard
nodemon --watch 'src/**/*.ts' --exec 'node out/index.js'
or
nodemon --watch src --exec 'node out/index.js'
Doesn't reload the app.
Solved!
By running nodemon in verbose mode I have discovered that by default it watches only *.js files, regardless of what wildcard you are watching. Therefore adding -e ts to the command fixes the problem:
nodemon --watch src/ --exec 'node out/index.js' -e ts
If someone uses nodemon.json here is mine after fix:
{
"watch": ["src"],
"exec": "tsc && node out/index.js" ,
"ext": "js, json, ts, proto"
}
I didn't have any luck with src/ watching either. I'm watching files via nodemon --watch '**/*' this will finds any changes in the nested files
Use single quotation for multi-value args like `--exec' in the package.json script.
e.g. I changed "nodemon --exec npm run build-langs" to "nodemon --exec 'npm run build-langs'" and worked.
I install the nodemon for my node project.
And I use "--watch app" command to watch the file change.
When I modify other files, it works well, I can see the auto restart log on the screen ( Windows powershell ).
However, when I modify the template engine file (path: app/view/index.nj), they do not restart automaticlly.
How to resolve this problem?
Here is a part of "package.json" file
"scripts": {
"dev": "nodemon --watch app --ignore app/static/css --ignore app/static/js ./dev.js"
}
Nodemon only restarts if .js and .coffee files are changed by default. You have to pass the extension to the nodemon.
"scripts": {
"dev": "nodemon -e js,html,nj --watch app --ignore app/static/css --ignore app/static/js ./dev.js"
}
I want to setup nodemon to run the "prestart" script when server restarts, the reason, I'm using webpack and I want to build bundle.js every time a file changes, I'm not using webpack-dev-server cause I don't know how to set it up with an existing node app running, I got my backend on node.js, help on that will be appreciated.
The way I've been working is: run npm start every time I make a change on the code, but that too much effort.
Here's the scripts object at package.json so that you have an idea of what's going on:
"scripts": {
"bundle": "webpack --config webpack.config.js",
"prestart": "npm run bundle",
"start": "node server.js"
}
The way I've accomplished to do that is by creating a nodemon.json file with the exec property:
{
// ... Other conf (see an example in https://github.com/remy/nodemon/blob/master/doc/sample-nodemon.md)
"watch": [
"app" // Set your directories/files to watch
],
"exec": "npm run prestart && node server/index.js"
}