scttcper / qbittorrent

qBittorrent api wrapper
https://qbittorrent.vercel.app
MIT License
43 stars 14 forks source link

Error [ERR_REQUIRE_ESM]: require() of ES Module @ctrl\qbittorrent\dist\src\index.js from index.ts not supported #127

Closed mouro001 closed 6 months ago

mouro001 commented 6 months ago

When using this package in typescript I get the following error:

Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules\@ctrl\qbittorrent\dist\src\index.js from index.ts not supported.
Instead change the require of index.js in index.ts to a dynamic import() which is available in all CommonJS modules.
    at require.extensions.<computed> [as .js] (nvm\v20.12.2\node_modules\ts-node\dist\index.js:851:20)
    at Object.<anonymous> (index.ts:3:23)
    at m._compile (nvm\v20.12.2\node_modules\ts-node\dist\index.js:857:29) {
  code: 'ERR_REQUIRE_ESM'

System Information: Windows 11, NodeJS 20.12.2 (installed by nvm), Typescript 5.4.5 (installed by ts-node 10.9.2)

Example index.ts: ```typescript import { QBittorrent } from '@ctrl/qbittorrent'; const client = new QBittorrent({ baseUrl: 'http://localhost:8080/', username: 'admin', password: 'adminadmin', }); async function main() { const res = await client.getAllData(); console.log(res); } main(); ``` package.json: ```json { "name": "qbittest", "version": "1.0.0", "description": "", "main": "index.ts", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "@ctrl/qbittorrent": "^8.0.0" } } ``` tsconfig.json ```json { "compilerOptions": { "target": "ESNext", "module": "CommonJS", "rootDir": ".", "outDir": "./dist/", "strict": true, "moduleResolution": "Node", "experimentalDecorators": true, "esModuleInterop": true, "skipLibCheck": true, "allowSyntheticDefaultImports": true, "resolveJsonModule": true, "forceConsistentCasingInFileNames": true, "removeComments": true, "typeRoots": ["node_modules/@types"], "sourceMap": false, "baseUrl": "./", }, "files": ["index.ts"], "include": ["./**/*.ts"], "exclude": ["node_modules", "dist"] } ```
liangshen001 commented 6 months ago

The same problem!

scttcper commented 6 months ago

this package is esm, you'll have to use esm too or something like bun or the tsx package that will bundle it for you. setup esm in typescript

see section I'm having problems with ESM and TypeScript https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#im-having-problems-with-esm-and-typescript