webtorrent / webtorrent-hybrid

WebTorrent (with WebRTC support in Node.js)
https://webtorrent.io
MIT License
516 stars 98 forks source link

Cannot import in expressjs #147

Closed drakonkat closed 2 years ago

drakonkat commented 2 years ago

What version of this package are you using? 5.0.1

What operating system, Node.js, and npm version? Nodejs: 14.17.6 Npm: 6.14.15 SO: Windows 10

What happened? Give me error when i change from const WebTorrent = require('webtorrent'); to const WebTorrent = require('webtorrent-hybrid');

What did you expect to happen? Does not give error but works in hybrid way

Log of the error: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\Project\FE\webtorrent-express-api\node_modules\webtorrent-hybrid\index.js require() of ES modules is not supported. require() of D:\Project\FE\webtorrent-express-api\node_modules\webtorrent-hybrid\index.js from D:\Project\FE\webtorrent-express-api\routes\torrent.js is an ES module file as it is a .js file whose nearest parent package.json contain s "type": "module" which defines all .js files in that package scope as ES modules. Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from D:\Project\FE\webtorrent-express-api\node_modules\webtorrent-hybrid\package.json.

DiegoRBaquero commented 2 years ago

Latest webtorrent-hybrid is an ES module

Must be imported as such

On 31/12/2021, at 9:25 AM, Drakonkat @.***> wrote:

 What version of this package are you using? 5.0.1

What operating system, Node.js, and npm version? Nodejs: 14.17.6 Npm: 6.14.15 SO: Windows 10

What happened? Give me error when i change from const WebTorrent = require('webtorrent'); to const WebTorrent = require('webtorrent-hybrid');

What did you expect to happen? Does not give error but works in hybrid way

Log of the error: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\Project\FE\webtorrent-express-api\node_modules\webtorrent-hybrid\index.js require() of ES modules is not supported. require() of D:\Project\FE\webtorrent-express-api\node_modules\webtorrent-hybrid\index.js from D:\Project\FE\webtorrent-express-api\routes\torrent.js is an ES module file as it is a .js file whose nearest parent package.json contain s "type": "module" which defines all .js files in that package scope as ES modules. Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from D:\Project\FE\webtorrent-express-api\node_modules\webtorrent-hybrid\package.json.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.

drakonkat commented 2 years ago

Sorry, i didn't undestand do you exptect that is working? because i have the latest version but continue to give me the error "[ERR_REQUIRE_ESM]". Maybe i'm missing something about expressjs or something?

drakonkat commented 2 years ago

WIth the version 4.1.3 everythings works, but there is a way to support old require in the new version?

Wamy-Dev commented 1 year ago

how did you solve this?

drakonkat commented 1 year ago

how did you solve this?

You can use dynamic import