错误:用摩卡testing时找不到模块
我使用babeljs
来编写一个RPG引擎库。 我有两个文件:
dice.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
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; } }
当我用mocha
testing他们,我做这些import:
tests.js
var assert = require('assert'); var Amzhen = require('../Amzhen.js'); var random = require('random-js'); //tests here...
然而,当我运行testing,我得到这个:
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)
任何想法为什么dice
模块没有被发现?
我正在用babel src --out-file Amzhen.js && mocha
编译代码
你应该使用:
import Dice from './dice';
'dice'
不是已发布,已安装的Node.js模块的名称,而是本地文件,因此您应该使用./dice
和相应的path。
另请参阅node.js中的Module not found错误
- Mongooseasynchronous/等待与Koa卡住await Model.findOne()。exec()
- 运行摩卡testing会导致“意外的保留字”错误
- Webpack不解决全球安装的babel或babel-loader
- 错误:无法find预设“反应”安装时使用npm install –global babel-preset-react但工作没有全局标志
- Babel + Mocha堆栈跟踪报告错误的行号
- 在node.js(ES6 / Babel)中inputX和input*之间的区别是X?
- es6导出/导入多个类 – instanceof返回false
- 未捕获错误:定义不能间接使用
- 无法解决承诺时使用asynchronous与巴贝尔和ES6诺言等待