【问题标题】:import { expect } from 'chai'; SyntaxError: Cannot use import statement outside a module从'chai'导入{期望}; SyntaxError: 不能在模块外使用 import 语句
【发布时间】:2021-03-08 17:21:03
【问题描述】:

在测试其中一项功能时,我遇到了这种错误。有什么问题?

import { expect } from 'chai';
import { PathFinding } from './path-finding.js';

describe('aStar', () => {
  const o = 0; // intermediate points when going forwards (the trace)
  const u = 0; // turning points
  const s = 0;
  const f = 0;

  it('should find a valid path', () => {
    const graph = [
      [0, u, o, o, f],
      [u, 1, 1, 1, 1],
      [0, u, o, u, 0],
      [1, 1, 1, 1, u],
      [s, o, o, u, 0],
    ];
    const start = { x: 0, y: 4 };
    const finish = { x: 4, y: 0 };
    const path = PathFinding.aStar({ graph, start, finish });
    expect(path).to.eql([
      { x: 3, y: 4 },
      { x: 4, y: 3 },
      { x: 3, y: 2 },
      { x: 1, y: 2 },
      { x: 0, y: 1 },
      { x: 1, y: 0 },
      finish,
    ]);
  });

...

/Users/user/WebstormProjects/hsu/src/path-finding/path-finding.spec.js:1 从'chai'导入{期望}; ^^^^^^

SyntaxError: 不能在模块外使用 import 语句 在 wrapSafe (节点:内部/模块/cjs/loader:1018:16) 在 Module._compile (node:internal/modules/cjs/loader:1066:27) 在 Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10) 在 Module.load (node:internal/modules/cjs/loader:967:32) 在 Function.Module._load (节点:internal/modules/cjs/loader:807:14) 在 Module.require (node:internal/modules/cjs/loader:991:19) 在需要(节点:内部/模块/cjs/helpers:92:18) 在 Object.exports.requireOrImport (/Users/antongorshkov/WebstormProjects/hsu/node_modules/mocha/lib/esm-utils.js:20:12) 在 Object.exports.loadFilesAsync (/Users/antongorshkov/WebstormProjects/hsu/node_modules/mocha/lib/esm-utils.js:33:34) 在 Mocha.loadFilesAsync (/Users/antongorshkov/WebstormProjects/hsu/node_modules/mocha/lib/mocha.js:431:19) 在 singleRun (/Users/antongorshkov/WebstormProjects/hsu/node_modules/mocha/lib/cli/run-helpers.js:125:15) 在exports.runMocha (/Users/antongorshkov/WebstormProjects/hsu/node_modules/mocha/lib/cli/run-helpers.js:190:10) 在 Object.exports.handler (/Users/antongorshkov/WebstormProjects/hsu/node_modules/mocha/lib/cli/run.js:362:11) 在 /Users/antongorshkov/WebstormProjects/hsu/node_modules/yargs/lib/command.js:241:49

【问题讨论】:

标签: javascript chai


【解决方案1】:

Node.js 遵循 CommonJS 模块系统。 require

const { expect } = require('chai');
const { PathFinding } = require('./path-finding.js');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 2021-10-15
    • 2020-12-31
    相关资源
    最近更新 更多