简体   繁体   English

不使用 es6 语法无法导入 Chessjs

[英]Chessjs cannot be imported without using es6 syntax

In all of the chessjs documentation, const chess = require('chess');在所有 chessjs 文档中, const chess = require('chess'); is used.用来。

However, when I try to use this syntax, I get this error:但是,当我尝试使用此语法时,我收到此错误:

const Chess = require('chess');
              ^

Error [ERR_REQUIRE_ESM]: require() of ES Module is not supported.
Instead change the require of main.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> {
  code: 'ERR_REQUIRE_ESM'
}

Is there a solution for this?有解决方案吗?

The dynamic import() looks like this dynamic import()看起来像这样

async function loader(){
  const Chess = await import('chess')
  // things with Chess
}

As Evert mentioned, this can also cover the conditional import in an ESModule正如 Evert 提到的,这也可以涵盖 ESModule 中的条件导入

Although this isn't the solution to the general question (which is why I'm not marking this as the solution), I ended up just migrating away from the module chess which doesn't allow commonjs requires to the module chess.js which allows both.虽然这不是一般问题的解决方案(这就是为什么我没有将其标记为解决方案),但我最终只是从不允许 commonjs 需要的模块chess迁移到模块chess.js允许两者。

New code:新代码:

const { Chess }= require('chess.js');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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