Determine npm or yarn script run from module - node.js

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

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

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 to exec "prestart" script at package.json

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

Tell Node script to execute 2 commands

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

Resources