乐闻世界logo
搜索文章和话题

How to set environment variables from within package.json ?

5 个月前提问
3 个月前修改
浏览次数181

6个答案

1
2
3
4
5
6

package.json中,您通常不会直接设置环境变量,因为这个文件主要是用来定义项目的依赖、脚本和配置信息。然而,您可以在package.jsonscripts部分定义脚本命令,在执行这些命令时设置环境变量。

例如,如果您使用的是Unix-like系统(如Linux或MacOS),可以使用export命令设置环境变量,然后执行其他命令。如果您在Windows上,则可能需要使用set命令,或者使用跨平台的解决方案,比如cross-env

下面是一个如何在scripts部分设置环境变量的例子:

json
{ "name": "your-package", "version": "1.0.0", "scripts": { "start": "export NODE_ENV=production && node app.js", "start:windows": "set NODE_ENV=production && node app.js" } }

在上面的例子中,我们在start脚本中设置了NODE_ENV环境变量为production,然后执行了node app.js

使用cross-env,您可以写一个跨平台的脚本命令:

json
{ "name": "your-package", "version": "1.0.0", "scripts": { "start": "cross-env NODE_ENV=production node app.js" }, "devDependencies": { "cross-env": "^7.0.3" } }

在这个例子中,不管您在什么操作系统上,start脚本都会设置NODE_ENV环境变量为production,然后运行node app.js。注意,您需要先安装cross-env作为开发依赖(使用命令npm install --save-dev cross-env)。

这种方式是非常灵活的,允许您在不同的环境(开发、测试、生产等)中运行不同的命令。记住,这里设置的环境变量只会在脚本运行时存在,脚本执行完毕后,这些环境变量不会影响到其他的shell进程。

2024年6月29日 12:07 回复

Set the environment variable in the script command:

shell
... "scripts": { "start": "node app.js", "test": "NODE_ENV=test mocha --reporter spec" }, ...

Then use process.env.NODE_ENV in your app.

Note: This is for Mac & Linux only. For Windows refer to the comments.

2024年6月29日 12:07 回复

Just use NPM package cross-env. Super easy. Works on Windows, Linux, and all environments. Notice that you don't use && to move to the next task. You just set the env and then start the next task. Credit to @mikekidder for the suggestion in one of the comments here.

From documentation:

shell
{ "scripts": { "build": "cross-env NODE_ENV=production OTHERFLAG=myValue webpack --config build/webpack.config.js" } }

Notice that if you want to set multiple global vars, you just state them in succession, followed by your command to be executed.

Ultimately, the command that is executed (using spawn) is:

shell
webpack --config build/webpack.config.js

The NODE_ENV environment variable will be set by cross-env

2024年6月29日 12:07 回复

Because I often find myself working with multiple environment variables, I find it useful to keep them in a separate .env file (make sure to ignore this from your source control). Then (in Linux) prepend export $(cat .env | xargs) && in your script command before starting your app.

Example .env file:

shell
VAR_A=Hello World VAR_B=format the .env file like this with new vars separated by a line break

Example index.js:

shell
console.log('Test', process.env.VAR_A, process.env.VAR_B);

Example package.json:

shell
{ ... "scripts": { "start": "node index.js", "env-linux": "export $(cat .env | xargs) && env", "start-linux": "export $(cat .env | xargs) && npm start", "env-windows": "(for /F \"tokens=*\" %i in (.env) do set %i)", "start-windows": "(for /F \"tokens=*\" %i in (.env) do set %i) && npm start", } ... }

Unfortunately I can't seem to set the environment variables by calling a script from a script -- like "start-windows": "npm run env-windows && npm start" -- so there is some redundancy in the scripts.

For a test you can see the env variables by running npm run env-linux or npm run env-windows, and test that they make it into your app by running npm run start-linux or npm run start-windows.

2024年6月29日 12:07 回复

I just wanted to add my two cents here for future Node-explorers. On my Ubuntu 14.04 the NODE_ENV=test didn't work, I had to use export NODE_ENV=test after which NODE_ENV=test started working too, weird.

On Windows as have been said you have to use set NODE_ENV=test but for a cross-platform solution the cross-env library didn't seem to do the trick and do you really need a library to do this:

shell
export NODE_ENV=test || set NODE_ENV=test&& yadda yadda

The vertical bars are needed as otherwise Windows would crash on the unrecognized export NODE_ENV command. I don't know about the trailing space, but just to be sure I removed them too.

2024年6月29日 12:07 回复

Try this on Windows by replacing YOURENV:

shell
{ ... "scripts": { "help": "set NODE_ENV=YOURENV && tagove help", "start": "set NODE_ENV=YOURENV && tagove start" } ... }
2024年6月29日 12:07 回复

你的答案