Skip to content

TypeScript issues #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
philkunz opened this issue Apr 18, 2024 · 10 comments
Closed

TypeScript issues #132

philkunz opened this issue Apr 18, 2024 · 10 comments
Labels

Comments

@philkunz
Copy link

[email protected]/node_modules/socket.io-parser/build/cjs/index.d.ts (1,25): The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@socket.io/component-emitter")' call instead.

@darrachequesne
Copy link
Member

Hi! I wasn't able to reproduce, could you please provide your tsconfig.json config file?

@philkunz
Copy link
Author

@darrachequesne Sure:

{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"verbatimModuleSyntax": true
}
}

@plvo
Copy link

plvo commented Apr 24, 2024

hey, same error for me:

node_modules/.pnpm/[email protected]/node_modules/socket.io-parser/build/cjs/index.d.ts:1:25 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@socket.io/component-emitter")' call instead.

1 import { Emitter } from "@socket.io/component-emitter";
                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error in node_modules/.pnpm/[email protected]/node_modules/socket.io-parser/build/cjs/index.d.ts:1

my tsconfig.json:

{
    "compilerOptions": {
        "module": "NodeNext",
        "moduleResolution": "NodeNext",
        "target": "ES2020",
        "sourceMap": true,
        "outDir": "dist",
        "resolveJsonModule": true,
        "esModuleInterop": true,
        "allowArbitraryExtensions": true
    },
    "include": [
        "src/**/*"
    ],
}

I use pnpm and build with tsc:

{
  "name": "server",
  "version": "1.0.0",
  "main": "dist/index.js",
  "license": "MIT",
  "type": "module",
  "scripts": {
    "build": "tsc",
    "dev": "node dist/index.js",
    "start": "pnpm build && pnpm dev"
  },
  "dependencies": {
    "cors": "^2.8.5",
    "express": "^4.19.2",
    "socket.io": "^4.7.5"
  },
  "devDependencies": {
    "@types/node": "^20.12.7",
    "typescript": "^5.4.5"
  }
}

@TriangularCube
Copy link

TriangularCube commented Apr 25, 2024

Can confirm, also running into this problem.

Issue seems to be coming from the type declaration, and not the source file. So while TS is emitting to CJS, the declaration is getting picked up as importing from an ESM module.

@plvo
Copy link

plvo commented Apr 25, 2024

Update: I used "skipLibCheck": true on my tsconfig.json

source: https://www.typescriptlang.org/tsconfig/#skipLibCheck

@philkunz
Copy link
Author

@plvo ... You used the brute force method to make TypeScript shut up.

@plvo
Copy link

plvo commented Apr 25, 2024

@philkunz yes it's not the best solution, I use it because it's a small project, but if there are other solutions, I'm interested

darrachequesne added a commit to socketio/emitter that referenced this issue Apr 26, 2024
In order to fix the following issue:

> error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@socket.io/component-emitter")' call instead.
>
> 1 import { Emitter } from "@socket.io/component-emitter";

This problem was introduced in [1], when reworking the dual packaging.

Related: socketio/socket.io-parser#132

[1]: ba6b56d
@darrachequesne
Copy link
Member

@philkunz I was indeed able to reproduce the issue, thanks for providing your tsconfig.json file 👍

This should be fixed by socketio/emitter@e6aa1a3, included in @socket.io/[email protected].

Could you please check?

@philkunz
Copy link
Author

@darrachequesne I comment on the the commit above, but the issue seems fixed for now.

@philkunz
Copy link
Author

@darrachequesne Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants