我的问题在标题上。我写了一个这样的课:
export default class Vector3 {
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
// object's functions
magnitude() {
return Math.sqrt(this.x ** 2 + this.y ** 2 + this.z ** 2);
}非常基本,我想用在另一个:
import Vector3 from '../radiosity/vector3';
const v1 = new Vector3(1, 2, 3);
QUnit.test('magnitude()', function (assert) {
const result = v1.magnitude();
assert.equal(result, Math.sqrt(14), '|(1, 2, 3)| equals sqrt(14)');
});但是当我运行我的QUnit测试时,它给了我以下内容:
`SyntaxError: Cannot use import statement outside a module
at Module._compile (internal/modules/cjs/loader.js:892:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at run (/home/thomas/Code/GitKraken/Slow-light-Radiosity/node_modules/qunit/src/cli/run.js:55:4)
at Object.<anonymous> (/home/thomas/Code/GitKraken/Slow-light-Radiosity/node_modules/qunit/bin/qunit.js:56:2)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)我在互联网上看到这个错误非常流行,但我还没有找到解决方案。
如果有人能帮我弄清楚。
发布于 2020-04-07 16:07:04
当Node了解ES6时,您将导出一个CommonJS模块。
class Vector3 {
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
magnitude() {
return Math.sqrt(this.x ** 2 + this.y ** 2 + this.z ** 2);
}
}
module.exports = Vector3;然后您可以通过以下方式导入:
const Vector3 = require('../radiosity/vector3')更多信息
https://stackoverflow.com/questions/61084054
复制相似问题