【问题标题】:adding an environment variable for NodeJS in a javascript file在 javascript 文件中为 NodeJS 添加环境变量
【发布时间】:2018-05-30 08:49:38
【问题描述】:

我正在使用 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';
  };

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

我该如何解决这个问题?

【问题讨论】:

    标签: javascript node.js environment-variables


    【解决方案1】:

    引用错误NIGHTWATCH_CONFIG 未定义,因为在调用环境变量process.env 时需要指定。

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

    【讨论】:

      猜你喜欢
      • 2020-08-27
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-19
      • 2020-04-07
      相关资源
      最近更新 更多