简体   繁体   English

将变量传递到 package.json

[英]Passing variables into package.json

I'm working on React app.我正在开发 React 应用程序。 I need to pass a variable into package.json from.env file:我需要将一个变量传递给 package.json from.env 文件:

{"proxy": "ENV_VAR"}

Is it possible to do that?有可能这样做吗?

Also, we build app using docker.此外,我们使用 docker 构建应用程序。 Could we rewrite that key using docker anyhow?我们可以用 docker 重写那个密钥吗?

What is the end goal?最终目标是什么? Is it to define a process direction on build?是在构建时定义流程方向吗? If it is then you dont need to pass it to package.json but to read it in the build script.如果是,那么您不需要将其传递给 package.json 而是在构建脚本中读取它。 For example start the build task like this ( its doesn't in package.json or not )例如,像这样启动构建任务(它不在 package.json 中)

PROXY=SOME_PROXY npm run-script build

If its something like create-react-app did you check: this https://create-react-app.dev/docs/proxying-api-requests-in-development/如果它类似于 create-react-app 你检查过:这个https://create-react-app.dev/docs/proxying-api-requests-in-development/

So you can read the env所以你可以阅读环境

const proxy = require('http-proxy-middleware');
module.exports = function(app) {
  app.use(
    '/api',
    proxy({
      target: process.env.ENV_VAR,
      changeOrigin: true,
    })
  );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM