how to use nodemon to inherit from another script - node.js

I have a script that runs a nodejs app
package.json:
{
"scripts": {
"start": "node app.js --inspect --other-flags ....",
"start:inspect": "npm run start -- --inspect",
"start:watch": "nodemon app.js ..."
}
}
as you can see in the script "start:inspect" in inherit all flags from "start" and adds "--inspect"
I want to inherit all "start" flags into "start:watch" but with nodemon without manual copying
something like "start:watch": "npm run start -- nodemon"

You could do this with an environment variable for the command, like "start:watch": "export CMD=nodemon; npm run start" and "start": "$CMD app.js ..."

Related

nodejs run custom script before main script

I want to run ../my_dir/my_script.js each time when I run node index.js from actual app project folder.
In others words, I need my_script.js to be common in every app (A,B,C...N) and executing before index.js
Structure:
+my_dir
-my_script.js
+appA
+node_modules
-package.json
-index.js
+appB
+node_modules
-package.json
-index.js
my_script.js
console.log('my_script.js from parent directory STARTED');
package.json (similar for each appA, appB ... etc)
{
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
//I HAVE TRIED
"start": "node ../my_dir/my_script.js",
"postinstall": "npm run-script ../my_dir/my_script.js",
"preinstall": "npm run --prefix ../my_dir/my_script.js",
}
}
index.js (similar for each appA, appB ... etc);
console.log('index.js STARTED');
If I try inside appA node index.js I got index.js STARTED
If I try inside appA npm start I got my_script.js from parent directory STARTED
Expected: (running both):
my_script.js from parent directory STARTED
index.js STARTED
Any idea how to achieve that?
I don't think it is possible to automatically have a second script run when using the command line node command.
What you can do is either run both scripts manually
node ../my_dir/my_script.js && node index.js
or bundle them in your package.json
"start": "node ../my_dir/my_script.js && node index.js",
and execute with
npm start
Your postinstall and preinstall are not quite right, those get run when doing npm install (you can put the post and pre keywords before any command https://docs.npmjs.com/cli/v9/using-npm/scripts)
so you could also do
"start": "node index.js",
"prestart": "node ../my_dir/my_script.js"
and execute with
npm start
again

How do i run one script on development and another on production

"scripts": {
"start": "$env:NODE_ENV=\"development\"; nodemon server.js",
"start:prod": "$env:NODE_ENV=\"production\"; nodemon server.js"
},
I want to set the NODE_ENV variable to development when i run 'start' script and to production when i run 'start:prod' script but i get error that says:
$env:NODE__ENV="development"; nodemon server.js
The filename, directory name, or volume label syntax is incorrect.

How to chain custom script in package.json to call prestart mongod?

Trying to streamline my package.json and local development with a custom script to run Nodemon. I'm currently building an app with a front and back end I need to call mongod before start and before my custom in two tabs however I'm running into an issue.
mongod will only run in the terminal if the terminal path is set to local from testing and I've read:
Correct way of starting mongodb and express?
npm starts to execute many prestart scripts
How to npm start at a different directory
How do I add a custom script to my package.json file that runs a javascript file?
I can use prestart as:
"scripts": {
"prestart": "cd && mongod",
"start": "node app",
"nodemon": "./node_modules/.bin/nodemon app"
}
but I'm not seeing how I should chain a prestart with a custom scripts. When I try to chain it with nodemon as:
"scripts": {
"prestart": "cd && mongod",
"start": "node app",
"nodemon": "cd && mongod && ./node_modules/.bin/nodemon app"
},
Nodemon is fired first than mongodb crashes in my package.json when I call Nodemon as:
npm run nodemon
How can I start mongod before starting nodemon in my development process through one command in the package.json?

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

Determine npm or yarn script run from module

Lets say I have the following scripts in my package.json file:
{
"start": "node index.js",
"start-with-flag": "node index.js -f"
}
Insideindex.js I have a console.log(process.argv), and the scripts above output the following:
$ npm run start
[ '/usr/local/Cellar/node/8.4.0/bin/node',
'/Users/.../test_app/index.js' ]
$ npm run start-with-flag
[ '/usr/local/Cellar/node/8.4.0/bin/node',
'/Users/.../test_app/index.js',
'-f' ]
Is it possible to retrieve the value of the scripts which I ran (start or start-with-flag) inside of index.js?
You can access the script which is currently executed with the npm_lifecycle_event environment variable.
command
$ npm run start-with-flag
index.js
console.log(process.env.npm_lifecycle_event) // start-with-flag
package.json
{
"scripts": {
"start": "node index.js",
"start-with-flag": "node index.js -f"
}
}

Resources