How to use npm script directly? - javascript

A script in my package.json
"start": "cross-env NODE_ENV=development PORT=3000 CODE_SPLIT=0 node ./react-imvc/bin/www-babel-register",
I need to start --inspect, for some reason, I can not modify the package.json, such as
"start": "cross-env NODE_ENV=development PORT=3000 CODE_SPLIT=0 node --inspect ./react-imvc/bin/www-babel-register"
I want to use script directly, like this :
npm run "cross-env NODE_ENV=development PORT=3000 CODE_SPLIT=0 node --inspect ./react-imvc/bin/www-babel-register"
using npm run script, but it failed.
How can I use npm script like above?

assume that you want to run script with arg --inspect without changing the package.json:
npm run start -- --inspect
see:
$ npm run --help
npm run-script <command> [-- <args>...]
alias: run

So you want to execute the script you've got in your package.json which should look like this (after you've added the --inspect flag):
"scripts": {
"start": "cross-env NODE_ENV=development PORT=3000 CODE_SPLIT=0 node --inspect ./react-imvc/bin/www-babel-register"
}
This can be done by calling npm run start. Adding more scripts under another key lets you call them by using `npm run '.

Related

Accessing the script of package.json of a npm package from our project package.json file

I created a tool and registered it on npmjs.com named Yattex. It script looks something like this
`
"scripts": {
"clean:reports": "rm -R -f cypress/reports && mkdir cypress/reports && mkdir cypress/reports/mochareports ",
"pretest": "npm run clean:reports",
"scripts": "cypress run",
"combine-reports": "mochawesome-merge cypress/reports/mocha/*.json > cypress/reports/mochareports/report.json",
"generate-report": "marge cypress/reports/mochareports/report.json -f report -o cypress/reports/mochareports -- inline",
"posttest": "npm run combine-reports && npm run generate-report",
"yattex-tool": "node yattex-tool",
"decorator": "node yattex-tool/test-decorators",
"test": "npm run decorator && npm run scripts || npm run posttest || npm run yattex-tool"
},
`
Now, in another project i installed the package and now i dont know what to write in scrips(test) to run the above test command which is in the node_modules/yattex/package.json file
I tried yattex and node node_modules/yattex/package.json
But it doesn't work. I'm a newbie and i cant find any solution...
If you add a property
"workspaces": ["./node_modules/yattex"]
to the package.json file in your other project, then npm test --workspaces will execute the tests both in your other project and in the yattex project (which is a "workspace" as explained on the npm help run page).
If you want to run only the tests for yattex, use npm test --workspace=./node_modules/yattex.

not working "NODE_ENV=development nodemon server.js" commnd in window and showing error as below mention

NODE_ENV=development : The term 'NODE_ENV=development' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
Need win-node-env module
npm install -g win-node-env
For Windows
SET NODE_ENV=development
node app.js
For setting environment variables like that on Windows you can use package called cross-env.
You can install it by: npm install --save-dev cross-env. And then you have almost exact example from their docs:
{
"scripts": {
"build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"
}
}

Unable to run pm2 with npx babel?

I have a Node.js app that perfectly works with the following command:
npx babel-node dist/index.js
However, I'm not able to run this with pm2. When I do the following:
pm2 start --interpreter npx babel-node dist/index.js
Obviously, this does not work. And when I try:
pm2 start --interpreter babel-node dist/index.js
This does not work either, as my global babel is version 6.26.3 and my project babel is 7.0.0-0.
Is there a way to make pm2 to play nice with npx or maybe there is a way to upgrade the system wide babel to 7.0?
To solve this you need to create a build and a start script in your package.json:
"scripts": {
"build": "npx babel src -d dist",
"start": "npm run build && node dist/index.js"
},
And then run the pm2 using the start option:
pm2 start npm --name "app name" -- start

How to run npm script (package.json) written in an external js file?

I know it has been covered in different questions but mine is a bit different:
Sorry in advance if it sounds really noob.
this is the script in package.json:
"start": "nodemon ./index.js --exec \"node -r babel-register\"",
I replaced that with:
"start": "node scripts/start.js",
and in start.js, I do:
const { execSync } = require('child_process')
execSync('nodemon ../index.js --exec \"node -r babel-register\"')
which throws an error:
/bin/sh: nodemon: command not found
Am I right with "execSync"?
I tried import nodemon in the file but it is obviously not helping.
What you're doing should work if nodemon is installed globally, i.e. with:
npm install -g nodemon
But if it's installed as a project dependency, i.e. with:
npm install --save-dev nodemon
Then you'll need to run it from the directory containing all the locally installed binaries: node_modules/.bin/
So something like this should work:
execSync('./node_modules/.bin/nodemon ../index.js --exec \"node -r babel-register\"')

How can I run multiple NPM commands with a single NPM Command on Windows

I am setting a package.json file that will start Nodemon, run my watch css command and run browser sync all with the "npm start" command.
This works on my Mac computer at work but does not work on my Windows computer at home.
On my Mac, it will listen for any changes to server and SCSS files and also run browser sync.
On Windows it only runs Nodemon and just waits for any server changes. It looks like it ignores my other two commands.
Do I need to do something different for Windows? Ideally I would like the code to work universally on both platforms.
Nodemon seems to be the problem here because watch-css css and browsersync work but anything after nodemon does not work.
"scripts": {
"build-css": "node-sass --output-style compressed --source-map true -o public/css scss",
"watch-css": "nodemon -e scss -x \"npm run build-css\"",
"build-js": "browserify js/app.js -o public/js/app.js",
"browser-sync": "browser-sync start --port 4000 --proxy 'localhost:3000' --files 'views/*' 'public/**/*' --no-notify",
"start": "nodemon ./bin/www & npm run watch-css & npm run browser-sync"
},
Here's what I use: npm-run-all(this is cross-platform). They allow you to run your processes/commands parallelly and sequentially (-p or -s).
"scripts": {
"build-css": "node-sass-chokidar src/ -o src/ --importer=node_modules/node-sass-tilde-importer",
"watch-css": "npm run build-css && node-sass-chokidar src/ -o src/ --watch --recursive",
"start-js": "react-scripts start",
"start": "npm-run-all -p watch-css start-js",
// ... and much more
}
This is working fine for me in both windows and mac. Try it, hope this is helpful.

Categories