webtorrent / webtorrent

⚡️ Streaming torrent client for the web

Home Page:https://webtorrent.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

require('webtorrent') not working in nodejs: "Error [ERR_REQUIRE_ESM]: require() of ES Module"

Xikee2023 opened this issue · comments

I run webtorrent with node, I can't use require('webtorrent'), it occurs error:
image

Code in test.js:

const Webtorrent = require('webtorrent');
console.log(Webtorrent);

WebTorrent Version:2.1.12
Node Version:18.16.0

Error details:

const Webtorrent = require('webtorrent');
                   ^

Error [ERR_REQUIRE_ESM]: require() of ES Module E:\APP\webtorrent\node_modules\webtorrent\index.js from E:\APP\webtorrent\test.js not supported.
index.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in E:\APP\webtorrent\node_modules\webtorrent\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Object.<anonymous> (E:\APP\webtorrent\test.js:1:20) {
  code: 'ERR_REQUIRE_ESM'
}

I can't seem to find anyone in the community asking a related question, and this seems like an obvious bug. (Or it's not a bug, but I didn't understand the usage of Webtorrent in node.)

commented

Webtorrent is now ESM only since version 2.0.0. You can take a look at the CHANGELOG.md