简体   繁体   中英

import { expect } from 'chai'; SyntaxError: Cannot use import statement outside a module

While testing one of the functionality, I faced with this kind of error. What is the problem?

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 import { expect } from 'chai'; ^^^^^^

SyntaxError: Cannot use import statement outside a module at wrapSafe (node:internal/modules/cjs/loader:1018:16) at Module._compile (node:internal/modules/cjs/loader:1066:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10) at Module.load (node:internal/modules/cjs/loader:967:32) at Function.Module._load (node:internal/modules/cjs/loader:807:14) at Module.require (node:internal/modules/cjs/loader:991:19) at require (node:internal/modules/cjs/helpers:92:18) at Object.exports.requireOrImport (/Users/antongorshkov/WebstormProjects/hsu/node_modules/mocha/lib/esm-utils.js:20:12) at Object.exports.loadFilesAsync (/Users/antongorshkov/WebstormProjects/hsu/node_modules/mocha/lib/esm-utils.js:33:34) at Mocha.loadFilesAsync (/Users/antongorshkov/WebstormProjects/hsu/node_modules/mocha/lib/mocha.js:431:19) at singleRun (/Users/antongorshkov/WebstormProjects/hsu/ node_modules/mocha/lib/cli/run-helpers.js:125:15) at exports.runMocha (/Users/antongorshkov/WebstormProjects/hsu/node_modules/mocha/lib/cli/run-helpers.js:190:10) at Object.exports.handler (/Users/antongorshkov/WebstormProjects/hsu/node_modules/mocha/lib/cli/run.js:362:11) at /Users/antongorshkov/WebstormProjects/hsu/node_modules/yargs/lib/command.js:241:49

Node.js follows the CommonJS module system. require

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM