module.js在运行iron-node时找不到模块

时间:2016-05-12 08:37:33

标签: javascript node.js mocha iron-node

我正在尝试使用iron-node(v2.2.17)来调试我的mocha单元测试。当我从package.json运行此命令时,单元测试运行正常:

"test": "cross-env NODE_ENV=test mocha test/.setup.js --reporter progress --compilers js:babel-core/register --require babel-polyfill --recursive \"./src/**/*.spec.js\" \"./src/**/*.integrationSpec.js\" \"./test/**/*.spec.js\"",

但是,当我运行此命令时,测试失败:

"debug:test": "cross-env NODE_ENV=test iron-node node_modules\\mocha\\bin\\_mocha test/.setup.js --reporter progress --compilers js:babel-core/register --require babel-polyfill --recursive \"./src/**/*.spec.js\" \"./src/**/*.integrationSpec.js\" \"./test/**/*.spec.js\"",

控制台中的错误是:

Error: Cannot find module 'src/framework/api/entityAddresses/entityAddressesAc  tions'

  - module.js:16 require
    internal/module.js:16:19

  - entityAddressesActions.spec.js:5 Object.<anonymous>
    entityAddressesActions.spec.js:5:1

此模块位于C:\TFS\Dev-UI\WebApp\Src\Web\Web\src\framework\api\entityAddresses\entityAddressesActions.js

我的项目文件夹是C:\TFS\Dev-UI\WebApp\Src\Web\Web,我的process.env.NODE_PATH也是C:\TFS\Dev-UI\WebApp\Src\Web\Web。 (它未定义但我在process.cwd()中将其设置为.iron-node.js - 请参阅https://github.com/s-a/iron-node/issues/98#issuecomment-218658236

我无法弄清楚为什么找不到该模块。我错过了一些配置吗?

修改

此SO答案建议,一旦应用启动,设置process.env.NODE_PATH将无效,因为模块路径已缓存:Determine project root from a running node.js application

我不知道如何在铁节点启动之前设置它,它始终是未定义的

1 个答案:

答案 0 :(得分:1)

可以使用.iron-node.js配置文件解决问题:

process.env.NODE_PATH = process.cwd(); // fix problem where modules are not resolved - Jack Allan.
var path = require("path");
var settings = {
  "nodeModule" : {
     "scriptInjection" : "module.paths.push(process.env.NODE_PATH);",  // fix problem where modules are not resolved - Jack Allan.
  },
  "v8": {
    "flags" : [                             // DEFAULT=[]; https://github.com/thlorenz/v8-flags/blob/master/flags-0.11.md
    ]
  },
  "app": {
    "native+"               : false,   // DEFAULT=FALSE; extends require to search native modules respecting the current v8 engine version.
    "autoAddWorkSpace"      : false,  // DEFAULT=TRUE; disables the autoAddWorkSpace behavior.
    "openDevToolsDetached"  : false,  // DEFAULT=FALSE; opens the dev tools windows detached in an own window.
    "hideMainWindow"        : false,  // DEFAULT=FALSE;  hides the main window to show dev tools only.
  },
  "workSpaceDirectory"        : function(argv) {  // determines the workspace directory for specific commandline applications.
    var result = "";
    if (argv[2]){
      result = path.dirname(argv[2]);
      var startupScriptName = path.basename(argv[2]).toLowerCase();

      switch(startupScriptName) {
          case "_mocha":
            result = process.cwd();
            break;
          default:
            result = path.resolve(result);
            break;
      }
    }

    return result;
  }
};

module.exports = settings;

感谢s-a建议:https://github.com/s-a/iron-node/issues/98#issuecomment-218712907