javascript 错误:用 mocha 测试时找不到模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28931904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:47:21  来源:igfitidea点击:

Error: cannot find module when testing with mocha

javascriptnode.jsecmascript-6babeljs

提问by Joel A. Villarreal Bertoldi

I'm using babeljsto write an RPG engine library. I have two files:

我正在使用babeljs编写 RPG 引擎库。我有两个文件:

dice.js

骰子.js

import assert from 'assert';
import Random from 'random-js';

export default class Dice {
  constructor(maxNumber) {
    assert(typeof(maxNumber) === "number", "maxNumber must be a number");

    this._mt = Random.engines.mt19937();
    this.minNumber = 1;
    this.maxNumber = maxNumber;
  }

  makeThrow() {
    this._mt.autoSeed();
    return Random.integer(this.minNumber, this.maxNumber)(this._mt);
  }
}

throwManager.js

throwManager.js

import assert from 'assert';
import Dice from 'dice';
export default class ThrowManager {
  constructor(settings) {
    assert(settings.hasOwnProperty("numberOfDices"), "must set 'numberOfDices'");
    assert(settings.hasOwnProperty("maxNumberInDice"), "must set 'maxNumberInDice'");
    assert(settings.maxNumberInDice <= 1, "must have at least 1 dice");
    this.settings = settings;
  }
  execute() {
    var throwResults = [];
    for (var d = 1; d <= this.settings.numberOfDices; d++) {
      var dice = new Dice(this.settings.maxNumberInDice);
      throwResults.push(dice.makeThrow());
    };
    return throwResults;
  }
}

When I test them with mocha, I do these imports:

当我用 测试它们时mocha,我会执行这些导入:

tests.js

测试.js

var assert = require('assert');
var Amzhen = require('../Amzhen.js');
var random = require('random-js');
//tests here...

Yet when I run the tests, I get this:

然而,当我运行测试时,我得到了这个:


Error: Cannot find module 'dice'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/joel/Amzhen.js/Amzhen.js:47:28)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/joel/Amzhen.js/test/tests.js:2:14)

Any ideas why the dicemodule isn't being found?

为什么dice找不到模块的任何想法?

I'm compiling the code with babel src --out-file Amzhen.js && mocha

我正在编译代码 babel src --out-file Amzhen.js && mocha

回答by Timothy Gu

You should use:

你应该使用:

import Dice from './dice';

'dice'is not the name of a published, installed Node.js module, but a local file, so you should use ./dicewith an appropriate path.

'dice'不是已发布的、已安装的 Node.js 模块的名称,而是一个本地文件,因此您应该使用./dice适当的路径。

See also Module not found error in node.js

另请参阅node.js 中未找到模块错误