I want to use the webtorrent package inside an Express application in typescript, but no matter how I configure my tsconfig file I keep getting compilation errors
Error [ERR_REQUIRE_ESM]: require() of ES Module C...\node_modules\webtorrent\index.js from ...src\controllers\TorrentParser.ts 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 ...\node_modules\webtorrent\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
my current tsconfig.json
looks as follows:
{
"compilerOptions": {
"esModuleInterop": true,
"skipLibCheck": true,
"target": "es2022",
"allowJs": true,
"resolveJsonModule": true,
"moduleDetection": "force",
"strict": true,
"noImplicitAny": true,
"noUncheckedIndexedAccess": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "NodeNext",
"module": "NodeNext",
"outDir": "./dist",
"rootDir": "./src",
"types": ["node", "express", "webtorrent"],
"allowSyntheticDefaultImports": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"],
"ts-node": {
"files": true
}
}