Tell Node script to execute 2 commands - node.js

I start my app with node server.js command
And this works to run my tests:
"scripts": {
"test": "node node_modules/karma/bin/karma start test/karma.conf.js",
}
Now now, when I run the test script, I want it to first execute server.js before starting the tests.
But this doesn't work:
"scripts": {
"test": "node server.js node node_modules/karma/bin/karma start test/karma.conf.js",
}
How can I get my test script to do both?

You forgot the semi-colon after the first command:
"scripts": {
"test": "node server.js; node node_modules/karma/bin/karma start test/karma.conf.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

npm start is starting the server.js not my React app, how can I use it to start my app?

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

how to use nodemon to inherit from another script

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 ..."

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