簡體   English   中英

不使用 es6 語法無法導入 Chessjs

[英]Chessjs cannot be imported without using es6 syntax

在所有 chessjs 文檔中, const chess = require('chess'); 用來。

但是,當我嘗試使用此語法時,我收到此錯誤:

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'
}

有解決方案嗎?

dynamic import()看起來像這樣

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

正如 Evert 提到的,這也可以涵蓋 ESModule 中的條件導入

雖然這不是一般問題的解決方案(這就是為什么我沒有將其標記為解決方案),但我最終只是從不允許 commonjs 需要的模塊chess遷移到模塊chess.js允許兩者。

新代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM