Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NPM config variables cross platform (win/linux)?

When using npm_package_config_<variable> usage is different per OS.

package.json (Linux & Windows)

config: {
    foo: "bar"
}

Then for usage:

Linux

node app.js --arg=$npm_package_config_foo

Windows

node app.js --arg=%npm_package_config_foo%

Thus I create 2 seperate scripts in my package.json but this feels cumbersome.

Any way to get this cross-platform?

P.S. I'm aware of the cross-env plugin, but this does not work for the above case, only for NODE_ENV.

UPDATE

To avoid misinterpretation of the context, here is a real case scenario I'm looking for, to use this for Docker commands to specify port numbers:

package.json

"config": {
    "port": "3000"
}
...
"scripts": {
    "docker:build": "docker build --build-arg PORT=$npm_package_config_port -t my-app .",
    "docker:build:win": "docker build --build-arg PORT=%npm_package_config_port% -t my-app .",
}
like image 345
Nicky Avatar asked Feb 10 '17 18:02

Nicky


People also ask

What is NPM cross-env?

cross-env-os Run scripts that set and use environment variables across platforms. cross-environment. environment variable.


1 Answers

You should use cross-var.

Then you can simplify to:

"scripts": { "docker:build": "cross-var docker build --build-arg PORT=$npm_package_config_port -t my-app .", }

like image 176
Mark Woon Avatar answered Oct 17 '22 08:10

Mark Woon