首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从“chai”导入{ expect };SyntaxError:不能在模块外部使用导入语句

从“chai”导入{ expect };SyntaxError:不能在模块外部使用导入语句
EN

Stack Overflow用户
提问于 2020-11-26 03:43:18
回答 1查看 687关注 0票数 4

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

代码语言:javascript
复制
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”导入{ expect };^

SyntaxError:无法在位于wrapSafe (节点:内部/模块/cjs/加载器:1018:16)、位于Module._compile (节点:内部/模块/cjs/加载器:1066:27)、位于Object.Module._extensions..js (节点:内部/模块/cjs/加载器:1131:10)、位于Module.load (节点:内部/模块/cjs/加载器:967:32)、位于Function.Module._load (节点:内部/模块/cjs/加载器:807:14)的Function.Module._load(节点:内部/模块/cjs/加载器:807:14)处的模块之外使用导入语句/modules/cjs/loader:991:19) at require (节点:internal/modules/cjs/helpers:92:18) at Object.exports.requireOrImport (/Users/antongorshkov/WebstormProjects/hsu/node_modules/mocha/lib/esm-utils.js:33:34) at Mocha.loadFilesAsync (/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/mocha.js:431:19) at /Users/antongorshkov/WebstormProjects/hsu/node_modules/yargs/lib/command.js:241:49 at singleRun (/Users/antongorshkov/WebstormProjects/hsu/node_modules/mocha/lib/cli/run.js:362:11) at Object.exports.handler (/Users/antongorshkov/WebstormProjects/hsu/node_modules/mocha/lib/cli/run-helpers.js:125:15) at singleRun(/Users/antongorshkov/WebstormProjects/hsu/node_modules/mocha/lib/cli/run.js:362:11)at Object.exports.handler (/Users/antongorshkov/WebstormProjects/hsu/node_modules/mocha/lib/cli/run-helpers.js:190:10) at exports.runMocha Object.exports.handler

EN

回答 1

Stack Overflow用户

发布于 2020-11-26 03:48:54

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

代码语言:javascript
复制
const { expect } = require('chai');
const { PathFinding } = require('./path-finding.js');
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65011697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档