是否可以在npm / package.json中使用环境变量?

时间:2021-01-28 23:14:19

I'm attempting to build a package.json so that when running a NodeJS app on Heroku it will run the scripts.postinstall step using an environment variable. For example:

我正在尝试构建一个package.json,这样当在Heroku上运行NodeJS应用程序时,它将使用环境变量运行scripts.postinstall步骤。例如:

...
"scripts": {
  "postinstall": "command $ENV_VAR"}
},
...

I've looked at the docs and wasn't able to find something saying I can.

我看过文档,却找不到我能说的话。

Is this even possible? Is this even desirable and "I'm Doing It Wrong"™?

这有可能吗?这甚至是可取的吗?“我做错了”™?

2 个解决方案

#1


7  

To answer the last questions, because they're the most important one: yes, no, and absolutely, because you've just broken cross-platform compatibility. There is no guarantee your environment syntax works for all shells on all operating systems, so don't do this.

回答最后一个问题,因为它们是最重要的问题:是,不,绝对,因为你刚刚破坏了跨平台的兼容性。无法保证您的环境语法适用于所有操作系统上的所有shell,因此请勿执行此操作。

We have a guaranteed cross-platform technology available to us already: Node. So, create a file called something like bootstrap.js, and then make npm run node bootstrap as your postinstall script. Since the code inside bootstrap.js will run like any other node script, it'll have access to process.env in a fully cross-platform compatible way, and everyone will be happy.

我们已经为我们提供了有保证的跨平台技术:Node。因此,创建一个名为bootstrap.js的文件,然后将npm run node bootstrap作为postinstall脚本。由于bootstrap.js中的代码将像任何其他节点脚本一样运行,因此它将以完全跨平台兼容的方式访问process.env,并且每个人都会很高兴。

And many, many, many things that use common utils have node equivalents, so you can npm install them, locally rather than globally, and then call them in an npm script. For instance mkdir -p is not cross-platform, but installing the mkdirp module is, and then an npm script like "ensuredirs": "mkdirp dist/assets" works fine everywhere when run as npm run ensuredirs

许多很多使用常见工具的东西都有节点等价物,所以你可以在本地而不是全局安装它们,然后在npm脚本中调用它们。例如mkdir -p不是跨平台的,但是安装mkdirp模块是,然后像“ensureirs”这样的npm脚本:“mkdirp dist / assets”在运行时运行为npm run ensureirs

And for convenience, the most common unix utilities have their own runner package, shx, which is fully cross-platform and makes the lives of devs even easier.

为了方便起见,最常见的unix实用程序有自己的运行程序包shx,它是完全跨平台的,使开发人员的生活更加轻松。

#2


6  

Ignore the nay-sayers. You can do this in a cross-platform manner using cross-var:

忽略那些不说话的人。您可以使用cross-var以跨平台方式执行此操作:

"scripts": {
    "postinstall": "cross-var command $ENV_VAR"
}

#1


7  

To answer the last questions, because they're the most important one: yes, no, and absolutely, because you've just broken cross-platform compatibility. There is no guarantee your environment syntax works for all shells on all operating systems, so don't do this.

回答最后一个问题,因为它们是最重要的问题:是,不,绝对,因为你刚刚破坏了跨平台的兼容性。无法保证您的环境语法适用于所有操作系统上的所有shell,因此请勿执行此操作。

We have a guaranteed cross-platform technology available to us already: Node. So, create a file called something like bootstrap.js, and then make npm run node bootstrap as your postinstall script. Since the code inside bootstrap.js will run like any other node script, it'll have access to process.env in a fully cross-platform compatible way, and everyone will be happy.

我们已经为我们提供了有保证的跨平台技术:Node。因此,创建一个名为bootstrap.js的文件,然后将npm run node bootstrap作为postinstall脚本。由于bootstrap.js中的代码将像任何其他节点脚本一样运行,因此它将以完全跨平台兼容的方式访问process.env,并且每个人都会很高兴。

And many, many, many things that use common utils have node equivalents, so you can npm install them, locally rather than globally, and then call them in an npm script. For instance mkdir -p is not cross-platform, but installing the mkdirp module is, and then an npm script like "ensuredirs": "mkdirp dist/assets" works fine everywhere when run as npm run ensuredirs

许多很多使用常见工具的东西都有节点等价物,所以你可以在本地而不是全局安装它们,然后在npm脚本中调用它们。例如mkdir -p不是跨平台的,但是安装mkdirp模块是,然后像“ensureirs”这样的npm脚本:“mkdirp dist / assets”在运行时运行为npm run ensureirs

And for convenience, the most common unix utilities have their own runner package, shx, which is fully cross-platform and makes the lives of devs even easier.

为了方便起见,最常见的unix实用程序有自己的运行程序包shx,它是完全跨平台的,使开发人员的生活更加轻松。

#2


6  

Ignore the nay-sayers. You can do this in a cross-platform manner using cross-var:

忽略那些不说话的人。您可以使用cross-var以跨平台方式执行此操作:

"scripts": {
    "postinstall": "cross-var command $ENV_VAR"
}