nodemon in npm script triggered multiple times - node.js

My npm scripts:
"build": "tsc -w -p ./src/server",
"run": "nodemon --watch ./dist/server ./dist/server/app.js",
"start": "concurrently --kill-others \"npm:build\" \"npm:run\""
From VSCode's terminal I can start the app using the start task.
But nodemon starts twice, and restarts multiple times when a file is saved. I assumed it's because the build task hasn't completed yet.
How can I make these work in series, so the one waits for the other? I do not want to use polling.
I'm using Ubuntu 18, node 10.15.0, npm 6.5.0.

Example of my configuration which works well:
package.json:
"start:dev": "nodemon --config nodemon.json ./dist/src/index.js",
nodemon.json:
{
"watch": ["src"],
"ext": "ts",
"ignore": ["src/**/*.spec.ts"],
"exec": "ts-node ./src/index.ts"
}

Related

npm start json-server in background

I'm trying to have my npm start script run json-server in the background in parallel to then running react-scripts to start UI. How would I go about doing this? I've tried various things with single & and prestart, etc but everything results in json-server starting to watch my db file but then react-scripts doesn't run.
The main 2 which I thought should do the trick are below, but neither work.
"scripts": {
"prestart" : "json-server --watch ../sample-db.json --port 3010 --delay 800&",
"start": "set HTTPS=true&&react-scripts start",
and
"scripts": {
"start": "json-server --watch ../sample-db.json --port 3010 --delay 800&set HTTPS=true&&react-scripts start",
Not quite a duplicate, but as indicated by #Max this has a few options:
How can I run multiple npm scripts in parallel?
So here's one way:
"scripts": {
"start": "set HTTPS=true&&react-scripts start",
"mockserver": "json-server --watch ../sample-db.json --port 3010 --delay 800",
"startdev": "concurrently --kill-others \"npm start\" \"npm run mockserver\"",
}
And I see that on windows I need to use "start" which was why single ampersand wasn't working.

Use of pm2 with npm run

I havea react frontendapp for which I defined a number of build and run tasks in package.json as shown in the following snippet:
"scripts": {
"start": "env-cmd -f .env.dev react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"build:dev": "env-cmd -f .env.dev npm run build",
"build:test": "env-cmd -f .env.test npm run build",
"build:prep": "env-cmd -f .env.prep npm run build",
"build:prod": "env-cmd -f .env.prod npm run build",
"start-dev-server": "env-cmd -f .env.dev node server/server.js",
"start-test-server": "env-cmd -f .env.test node server/server.js",
"start-prep-server": "env-cmd -f .env.prep node server/server.js",
"start-server": "env-cmd -f .env.prod node server/server.js"
},
My goal is to run the app by using the pm2 tool based on a run configuration which should execute "npm run start-server". This execution should internaly run the last line in the above snippet which is "env-cmd -f .env.prod node server/server.js"
I wrote a pm2 config file, namely my_config.json, for to realizing above sketched scenario as follows:
my_config.json:
{
"apps": [
{
"name": "ReactFrontEndApp",
"script": "npm",
"args": "run start-server"
}
]
}
Finally, I issue the following command in the command console:
pm2 start my_config.json
When above pm2 command is issued my application is listed under pm2 list command output. Nevertheless, the pm2 tool also starts popping up command-consoles one after another without an end (the previos one fades away and then the next one appears). I tested my application at https://localhost:3000 in the browser meanwhile but the browser does not bring anything my app at all (i.e. not found). As a consequence, I have to stop my application by using pm2 stop command.
QUESTION: Is there something I might be missing in the my_config.json file, which might be causing this successive command console opennings? What could be wrong?
I guess that your server fail to load somehow, maybe pm2 change relevant environment variables.
what is the output of pm2 logs 0? what happened when you run npm run start-server manually? what happened when run pm2 start npm -- run start-server manually?

Nodemon ''npm' is not recognized as an internal or external command

I realize that this is most likely a duplicate question. I'm new to nodemon and I'm trying to establish a server for a Vue JS project with nodemon. I'm trying to run eslint with nodemon and can't figure out why I keep getting the error message. If I remove npm after --exec it will tell me ''run' is not recognized, and if I remove that I will get ''lint' is not recognized and so on.
My package.json file:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon src/app.js --exec 'npm run lint && node'",
"lint": "eslint **/*.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"eslint": "^4.16.0",
"nodemon": "^1.14.12"
}
}
I have also tried this code in my start scripts:
"scripts" : {
"start": "./node_modules/nodemon/bin/nodemon.js src/app.js --exec 'npm run lint && node'",
"lint": "./nodemodules/.bin/eslint **/*.js"
}
Where is tells me that "." is not recognized as an internal external command.
I've installed nodemon into my server folder and in the project directory as well as globally. I have done the same with eslint as well.
i had the same problem today. did some google stuff and found that this is not working anymore. so i tried this
"scripts": {
"prestart": "npm run lint ",
"start": "nodemon src/app.js ",
"lint": "./node_modules/.bin/eslint src/*.js"
},
when you npm start node will run the pre-start script before the start script.Once a file being updated this pre-start wont run by the nodemon.So for that we have to call the nodemon events.So create a nodemon.json on root folder and paste following.
{
"events": {
"restart": "npm run lint"
}
}
you can read more nodemon config options from here nodemon config .There are more nodemon events.you can read them from here event restart
PS:im very new to this. :)
EDIT1:
You can use as follows. this dont need a nodemon config;
"scripts": {
"start": "node src/app.js",
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon src/app.js --exec \"npm run lint --fix && node\"",
"lint": "eslint --fix **/*.js "
}
for run use npm run dev it will run es lint + nodemon. this is for windows cmd command.if you are using bash terminal, remove \ in "dev" ;
"dev": "nodemon src/app.js --exec "npm run lint --fix && node""
I had the same problem.
For some reason you can't use simple quotes in npm scripts.
Use escaped double quotes instead. This should work:
"start": "nodemon src/app.js --exec \"npm run lint && node\""
install it globally for making it available on path.
npm i -g nodemon
or if using yarn
yarn global add nodemon
and if you tried this approach and it didn't work.
you should try running it locally..
you have to create a script in your package.json like this
"script": {
"server" : "nodemon scriptFile.js" //name of the file you want to run
}
then use,
npm run server
but before it,
install nodemon locally.
check it, if it is available on package.json

Concurrently does not modify NODE_ENV variable

So I'm working on a project using webpack and wanted to create a script on my package.json to run both dev and production mode from there. I'm a windows user and always use Concurrently to run multiple terminal tasks at the same time.
I set my package.json scripts like this:
"scripts": {
"start": "concurrently \"set NODE_ENV=\" \"webpack --watch\"",
"build": "concurrently \"set NODE_ENV=production\" \"webpack\""
},
The output of this in the terminal is:
set NODE_ENV= exited with code 0
Webpack is watching the files…
...
So basically webpack is working properly, but the variable is not being created/deleted. Both commands are failing.
If I run directly
set NODE_ENV=production
it works, so I'm a bit confused...
Any ideas?
Thanks a lot!
Change:
"start": "concurrently \"set NODE_ENV=\" \"webpack --watch\"",
"build": "concurrently \"set NODE_ENV=production\" \"webpack\""
to:
"start": "NODE_ENV= webpack --watch",
"build": "NODE_ENV=production webpack"
You cannot change the environment in one process and expect it to be changed in another started in parallel. You can only change the env of child processes and only on their startup. Child process always inherits the environment from the parent.
If the above doesn't work on Windows then use cross-env:
npm install --save-dev cross-env
and in package.json use:
"start": "cross-env NODE_ENV= webpack --watch",
"build": "cross-env NODE_ENV=production webpack"

npm: how to run test & lint on each change?

I am using a bare npm ( no grunt/gulp) approach to develop my new MEAN project.
My config is like the following:
File package.json:
...
"start": "nodemon ./bin/www",
"lint": "jshint **/*.js",
"test": "mocha --recursive",
"dependencies": {
...
},
"devDependencies": {
...
},
Before starting, I run an npm start and nodemon starts monitoring my project tree for changes, triggering a restart after each source file change.
So far so good.
But what if I'd like to include - say - the lint and/or the test stages on each restart?
I didn't find any clue nor in the nodemon page nor in the npm one...
So you should have a definition of the start in your package.json to first run lint, then test then the actual run server.
You can find an example in following post:
http://substack.net/task_automation_with_npm_run
you should run the 'npm run monitor' command to start the monitoring and the restart should call the npm run start script.
but basically you want to have (based on your package.json)
"scripts": {
"start": "npm run lint & npm run test & node ./myfile.js",
"lint": "jshint **/*.js",
"test": "mocha --recursive",
"monitor": "nodemon ./bin/www"
.....

Resources