In npm how to pass env variable to another script command? - node.js

I have 2 commands and another 2 commands that invoke the first two but with different env variables.
but setting env variable doesn't work.
"build": "NODE_ENV=production npm run build:client && npm run build:server",
"build:tests": "NODE_ENV=tests npm run build:client && npm run build:server",
"build:client": "webpack --config webpack.conf.js",
"build:server": "webpack --config webpack.conf.js",
but sadly, inside conf.prod.js i can't access node_env because it was written inside another script (1 "above" it).
Thanks

Related

condition Script in package.json

I have two questions:
How I can do this npm run script --production instead npm run script --env=production
How I want to pass as an argument to another script something like, if you want to create database on development you just run npm run dev --development --create-database or if you wanna drop development database npm run dev --development --drop-database, this is because I want to add one script to handle create or drop database but if you are doing this in production database, it will throw you a warning..
This is what I have until now
"dev": "NODE_ENV=development nodemon src/serve.js",
"start": "NODE_ENV=production src/serve.js",
"test": "NODE_ENV=test jest --testTimeout=10000 --runInBand --detectOpenHandles",
"db:create": "NODE_ENV=$npm_config_env npm run db:condition",
"db:drop": "NODE_ENV=$npm_config_env npx sequelize-cli db:drop",
"db:condition": "if [[ ${NODE_ENV} == \"production\" ]]; then npm run warning; else npm run db:reset; fi",
"db:reset": "npx sequelize-cli db:drop && npx sequelize-cli db:create && npx sequelize-cli db:migrate && npx sequelize-cli db:seed:all",
"warning": "echo \"You can't do this on production\""
If you can see I only add condition for "db:create".

node js build problem with an error "'.' is not recognized"

I am learning Node and working on a existing project which has build script as below:
"build": "npm run clean && ./node_modules/.bin/webpack --env=production --progress --profile --colors --display-optimization-bailout"
I did the npm install as per the instruction, but the build command gives an error below:
$ npm run build
> npm run clean && ./node_modules/.bin/webpack --env=development --progress --profile --colors --display-optimization-bailout
> rimraf build
'.' is not recognized as an internal or external command,
operable program or batch file.
Any idea on what I could be missing and how I can troubleshoot the problem, I am on windows pc. Thanks!
Wrap the path to webpack in JSON escaped double quotes, i.e. \"...\".
For instance:
"build": "npm run clean && \"./node_modules/.bin/webpack\" --env=production --progress --profile --colors --display-optimization-bailout"
^^ ^^

Failed to write to file: ENOENT: no such file or directory

I am trying to build a folder with the name of build which will gonna contains number of map files and JavaScript files. But i'm getting an issue shown below.
Code :
"scripts": {
"prestart": "d2-manifest package.json manifest.webapp",
"start": "webpack-dev-server",
"test": "echo Everything probably works great\\! ## karma start test/config/karma.config.js --single-run true",
"build": "rm -rf build && set NODE_ENV=production webpack --progress && npm run manifest",
"postbuild": "cp -r src/i18n icon.png ./build/",
"validate": "npm ls --depth 0",
"manifest": "d2-manifest package.json build/manifest.webapp",
"deploy": "npm run build && mvn clean deploy",
"lint": "echo Looks good."
}
Error :
(I'm ignoring the fact that you seem to run this on a Windows machine)
The set command does not do what you think it does. To set an environment variable for a command, use
VARIABLE=value cmd
or
env VARIABLE=value cmd
This means changing
set NODE_ENV=production webpack --progress
into
env NODE_ENV=production webpack --progress
With set NODE_ENV=production webpack --progress you just set the positional parameters of the current shell instance to NODE_ENV=production, webpack and --progress.

How to set host environment variables inside npm scripts(package.json)

Imagine I have an environment variable
export NODE_ENV=production
when I do
echo $NODE_ENV //--> shows production which is correct
Problem:
Inside my package.json
scripts: {
...
"build": "export REACT_APP_NODE_ENV=${NODE_ENV:-development};
npm run build-css && react-scripts build",
...
}
Now when I do npm run build REACT_APP_NODE_ENV is getting set to development...but it should have been production as NODE_ENV is present.
If I do
scripts: {
...
"build": "export REACT_APP_NODE_ENV=production;
npm run build-css && react-scripts build",
...
}
It works correctly as expected i.e. all scripts access the REACT_APP_NODE_ENV with expected value that is production.
Goal
I wish to avoid hardcoding in my package.json
How can I set REACT_APP_NODE_ENV with value ${NODE_ENV}
"build": "export REACT_APP_NODE_ENV=${NODE_ENV};
npm run build-css && react-scripts build",
You probably want to ensure that this is cross-platform which will save you some headaches later on.
That problem has already been solved in the npm package cross-var.
Then, assuming you've already exported NODE_ENV, you use it this way:
"scripts": {
"build": "REACT_APP_NODE_ENV=${NODE_ENV}"
}

Npm scripts doesn't work the way I want

see below:
scripts": {
"build": "node_modules/.bin/babel sercer/src --out-dir server/dist ",
"build:watch": "node_modules/.bin/babel server/src --out-dir server/dist --watch",
"start:server": "node ./node_modules/nodemon/bin/nodemon.js ./server/dist/app.js",
"dev" : "(npm run build:watch) && (npm run start:server)"
}
you know, both of them can work well when I run npm run xxx , but when i conbian them like npm run dev does,the last one will not taking effect.what wrong with my script?
You could try
"dev" : "npm run build:watch && npm run start:server"
you can use the post- and pre- scripts that will be called before and after that script.
eg :
"build": "npm run build:css && npm run build:js",
"prebuild:js": "npm run lint"
In the above example build will execute both build:css and build:js - but not before running the lint task.

Resources