Skip to content

Typescript build error cjs #129

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
BenjaminPosselt opened this issue Jun 27, 2022 · 7 comments
Closed

Typescript build error cjs #129

BenjaminPosselt opened this issue Jun 27, 2022 · 7 comments

Comments

@BenjaminPosselt
Copy link

I just stumbled upon a weird error:
I am currently working on a typescript project which is using socket.io in its backend. (nodejs)

When trying to build it, I got this error:

node_modules/engine.io/build/engine.io.d.ts:3:25 - error TS7016: Could not find a declaration file for module 'engine.io-parser'. 'C:/Users/Lenovo/Code/wehub_development/wehub/node_modules/engine.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/engine.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'engine.io-parser';`

3 import * as parser from "engine.io-parser";
                          ~~~~~~~~~~~~~~~~~~

node_modules/engine.io/build/transport.d.ts:4:24 - error TS7016: Could not find a declaration file for module 'engine.io-parser'. 'C:/Users/Lenovo/Code/wehub_development/wehub/node_modules/engine.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/engine.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'engine.io-parser';`

4 import { Packet } from "engine.io-parser";
                         ~~~~~~~~~~~~~~~~~~


Found 2 errors in 2 files.

Errors  Files
     1  node_modules/engine.io/build/engine.io.d.ts:3
     1  node_modules/engine.io/build/transport.d.ts:4

When i tried copying the .d.ts files in your package from the esm folder into the cjs folder, it suddenly worked without complaint.

Could it be possible that you forgot to add the .d.ts files to the cjs export of your package or are my tsconfig configurations incorrect in some way?

Thank you! :)

@darrachequesne
Copy link
Member

Hi! Which version of engine.io-parser are you using? Could you please run npm ls engine.io-parser to display the dependency tree?

@BenjaminPosselt
Copy link
Author

This was the result:

@JumpLink
Copy link

JumpLink commented Jul 21, 2022

I have a similar problem using yarn:

➤ YN0000: total size is 132.334.529  speedup is 0,99
➤ YN0000: ../../.yarn/cache/engine.io-parser-npm-5.0.3-55c5b3beef-88d664420a.zip/node_modules/engine.io-parser/build/esm/commons.d.ts(1,23): error TS2688: Cannot find type definition file for 'node'.
➤ YN0000: ../../.yarn/cache/engine.io-parser-npm-5.0.3-55c5b3beef-88d664420a.zip/node_modules/engine.io-parser/build/esm/commons.d.ts(1,23): error TS2688: Cannot find type definition file for 'node'.
➤ YN0000: ../../.yarn/cache/engine.io-parser-npm-5.0.3-55c5b3beef-88d664420a.zip/node_modules/engine.io-parser/build/esm/commons.d.ts(7,40): error TS2591: Cannot find name 'Buffer'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
$ yarn info engine.io-parser -R
└─ engine.io-parser@npm:5.0.3
   ├─ Version: 5.0.3
   │
   └─ Dependencies
      └─ @socket.io/base64-arraybuffer@npm:~1.0.2 → npm:1.0.2

I can fix that if I install @types/node, but this is a frontend project and not a Node.js project

@darrachequesne
Copy link
Member

Closed due to inactivity, please reopen if needed.

@bjthehun
Copy link

bjthehun commented May 24, 2023

I've reencounted the same problem that @BenjaminPosselt described, so I'd like this isssue to be reopened. As context, I'm trying to convert an existing Node.js project with CommonJS modules into a project with ECMAScript Modules.

My package.json:

{
  "name": "my_project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "nodemon --exec 'ts-node-esm' index.ts",
    "build": "tsc --project ./"
  },
  "dependencies": {
    "downloadjs": "^1.4.7",
    "express": "^4.18.2",
    "socket.io": "^4.6.1",
    "uuid": "^9.0.0"
  },
  "devDependencies": {
    "@types/express": "4.17.17",
    "@types/node": "^18.16.7",
    "@types/serve-static": "^1.15.1",
    "@types/socket.io-parser": "^3.0.0",
    "@types/ws": "^8.5.4",
    "nodemon": "^2.0.22",
    "ts-node": "^10.9.1",
    "typescript": "^4.9.5"
  },
  "optionalDependencies": {
    ... omitted
  }
}

My tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "ES2022",
    "moduleResolution": "node16",
    //...
    "outDir": "./build",
    "esModuleInterop": true,
    "strict": true
  }
}

Running the server with npm run start does work, but I can't build it:

my_machine:~/my_project$ npm run build

> [email protected] build
> tsc --project ./

node_modules/engine.io/build/engine.io.d.ts:3:25 - error TS7016: Could not find a declaration file for module 'engine.io-parser'. '~/my_project/node_modules/engine.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/engine.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'engine.io-parser';`

3 import * as parser from "engine.io-parser";
                          ~~~~~~~~~~~~~~~~~~

node_modules/engine.io/build/socket.d.ts:5:25 - error TS7016: Could not find a declaration file for module 'engine.io-parser'. '~/my_project/node_modules/engine.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/engine.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'engine.io-parser';`

5 import { RawData } from "engine.io-parser";
                          ~~~~~~~~~~~~~~~~~~

node_modules/engine.io/build/transport.d.ts:4:24 - error TS7016: Could not find a declaration file for module 'engine.io-parser'. '~/my_project/node_modules/engine.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/engine.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'engine.io-parser';`

4 import { Packet } from "engine.io-parser";
                         ~~~~~~~~~~~~~~~~~~


Found 3 errors in 3 files.

Errors  Files
     1  node_modules/engine.io/build/engine.io.d.ts:3
     1  node_modules/engine.io/build/socket.d.ts:5
     1  node_modules/engine.io/build/transport.d.ts:4

Moving the .d.ts files into the cjs folder would fix the build process, but I don't want to do that for CI/CD.

The engine.io-parser version my project uses:

$ npm ls engine.io-parser
[email protected] ~/my_project
└─┬ [email protected]
  └─┬ [email protected]
    └── [email protected]

@darrachequesne
Copy link
Member

The declaration files are now included for the CommonJS build: e4a1c2b

Could you please check if that fixes the issue?

@bjthehun
Copy link

It does fix the issue. Thank you!

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

No branches or pull requests

4 participants