节点以编程方式设置导入模块不可用的进程环境变量

sxk*_*xkx 5 javascript import environment-variables node.js ecmascript-6

我是新手,希望这个问题的格式和制定得当.迫不及待地想在这个问题上看到你的答案.我们来吧..

上下文

上周末,我试图es2015create-react-app配置文件中实现语法支持,这是直截了当的.我所要做的就是使用babel-registerbabel-preset-env使其正常工作.到目前为止,你可以说这么好,但这并不是一件好事.经过几个小时的搜索,我发现process.env变量不会传递给导入的模块.下面的代码将演示我的问题.

代码

的package.json

{
  ...
  "scripts": [
    "good": "NODE_ENV=development BABEL_ENV=development node -r babel-register scripts/start.js",
    "bad": "node -r babel-register scripts/start.js"
  ],
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-preset-env": "^1.7.0",
    "babel-register": "^6.26.0"
  }
  ...
}
Run Code Online (Sandbox Code Playgroud)

.babelrc

{
  "presets": [ "env" ]
}
Run Code Online (Sandbox Code Playgroud)

脚本/ start.js

'use strict'

process.env.NODE_ENV = 'development';
process.env.BABEL_ENV = 'development';

// Always works
const a = require('../src/a');
// Only when environment variables are passed in via the CLI
import b from '../src/b';

console.log('Bye bye..');  
Run Code Online (Sandbox Code Playgroud)

SRC/a.js

'use strict'

console.log('Module A:', process.env.NODE_ENV);

const a = { name: "Module A" };
export default a;
Run Code Online (Sandbox Code Playgroud)

SRC/b.js

'use strict'

console.log('Module B:', process.env.NODE_ENV);

const b = { name: "Module B" };
export default b;
Run Code Online (Sandbox Code Playgroud)

运行代码

您将在下面看到两个npm脚本的输出:

npm run good
# Outputs:
Module B: development
Module A: development
Bye bye..

npm run bad
# Outputs:
Module B: undefined
Module A: development
Bye bye..
Run Code Online (Sandbox Code Playgroud)

我的问题

  1. 为什么没有以编程方式将环境变量传递给导入的模块?
  2. 这可以在保持es2015语法的同时修复吗?(例如使用babel插件?)

更多信息

只是将我process.env.NODE_PATH移到CLI将无法工作,以create-react-app编程方式在其配置/脚本文件中的多个位置设置环境变量.我在下面列出了几个链接,指向create-react-app回购和一些给我带来麻烦的文件.

笔记)

根据我目前的理解,create-react-app与我遇到的问题几乎没有关系.我很感兴趣为什么编程设置环境变量不会传递给导入的模块.

我的设置

  • 操作系统:Ubuntu 16.04
  • 节点:v8.11.2
  • Npm:6.3

Mar*_*yer 6

ES6 imports are hoisted. This means they will run before the rest of the code regardless of where they are in the source. The result is that b.js will run before you have set process.env.NODE_ENV = 'development'.

Babel's output will be consistent with this and will simulate hoisted imports by moving b's require statement to the top of the file. Babel will create a start file that looks like:

'use strict';

var _b = require('../src/b');

var _b2 = _interopRequireDefault(_b);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

process.env.NODE_ENV = 'development';
process.env.BABEL_ENV = 'development';

// Always works
var a = require('../src/a');
// Only when environment variables are passed in via the CLI
Run Code Online (Sandbox Code Playgroud)

看看为什么这不起作用应该很清楚。

[顺便说一句,很多人强烈建议您不要NODE_ENV在运行时设置]


sxk*_*xkx 5

回答我的第二个问题

感谢@Mark Meyer提供的见解,我能够让它发挥作用。

脚本/start.js

'use strict'  

import '../config/devEnv';
Run Code Online (Sandbox Code Playgroud)

配置/devEnv.js

'use strict'  

process.env.NODE_ENV = 'development';
process.env.BABEL_ENV = 'development';
Run Code Online (Sandbox Code Playgroud)

现在环境变量设置器也被提升,使它们可用于所有导入的模块。