在javascript文件中添加NodeJS的环境变量

时间:2018-05-30 08:49:38

标签: javascript node.js environment-variables

我正在使用Nightwatch对我的Vue js应用程序进行一些测试。由于我已将测试集成到我的gitlab ci / cd中,因此我需要两种不同的Nightwatch配置。

为了实现这一点,我做了以下事情:

为本地和gitlab测试创建两个不同的脚本

   "scripts" : {
    "e2e" :  "node test/e2e/gitlab_runner.js" ,
    "local-test" :  "node test/e2e/local_runner.js" ,
    "test": "npm run e2e"

然后我在local_runnergilab_runner文件

中嵌入了我需要的变量
process.env.NIGHTWATCH_CONFIG = 'nightwatch.local.js';
require('./runner.js');

gitlab_runner

process.env.NIGHTWATCH_CONFIG = 'nightwatch.conf.js';
require('./runner.js');

然后在文件runner.js中我有以下代码行

  opts = opts.concat(['--config', 'test/e2e/' + NIGHTWATCH_CONFIG]);

所以我可以得到正确的文件。

问题是当我运行脚本时出现以下错误:

(node:21061) UnhandledPromiseRejectionWarning: ReferenceError: NIGHTWATCH_CONFIG is not defined
    at devConfigPromise.then.then (xxxxxx/test/e2e/runner.js:30:51)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:182:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:697:11)
    at startup (internal/bootstrap/node.js:201:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:516:3)
(node:21061) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:21061) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我还尝试在local_runnergitlab_runner文件中执行以下操作:

  module.exports = {
    NIGHTWATCH_CONFIG = 'nightwatch.local.js';
  };

但是NIGHTWATCH_CONFIG文件中的runner.js变量未定义。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

未定义引用错误rfind,因为需要指定调用环境变量NIGHTWATCH_CONFIG

process.env