Skip to content

"Cannot find module 'http-status' or its corresponding type declarations." after migrating to 2.0.0 #53

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

Open
dschuessler opened this issue Nov 6, 2024 · 8 comments

Comments

@dschuessler
Copy link

Describe the bug

After upgrading to http-status 2.0.0, I get the following error on every import of the package.

src/controllers/metrics.controller.ts:2:24 – error TS2307: Cannot find module 'http-status' or its corresponding type declarations.

2 import httpStatus from 'http-status';
                         ~~~~~~~~~~~~~

To Reproduce

  1. Clone https://github.com/dschuessler/http-status-import-bug
  2. Run npm run build

Additional context

I consider this a bug since the README claims that, for ESM users, the import remains the same.

@gregorio-gerardi
Copy link

same here

@wdavidw
Copy link
Member

wdavidw commented Nov 7, 2024

It seems like you are using TypeScript. The repo http-status-import-bug provided above is configured for CommonJS.

  • no "type" field set to "module" in the package.json file, add
    {
      "type": "module"
    }
  • "module" field set to "commonjs" in the tsconfig.json, replace with
    {
      "compilerOptions": {
        "target": "esnext",
        "module": "NodeNext",
        "moduleResolution": "NodeNext",
     }
    }

@wouterds
Copy link

You can import it from /dist, so changing

import httpStatus from 'http-status';

to

import httpStatus from 'http-status/dist';

Will work, but ideally it would indeed just keep working as it was.

@wdavidw
Copy link
Member

wdavidw commented Nov 13, 2024

Please share your envionnement (Browser/Node.js, CommonJS/ESM, JS/TS, ...). For example, there shouldn't be a need to import /dist with Node.js.

@wouterds
Copy link

I'll look into a repro this evening, but TLDR I can't get it to work with CommonJS without importing from the /dist sub-folder.

@wdavidw
Copy link
Member

wdavidw commented Nov 13, 2024

With Node.js and without TypeScript ?

@wouterds
Copy link

wouterds commented Nov 13, 2024

Node.js 20, compiled with tsc from TS to JS

{
  "lib": ["ESNext"],
  "module": "CommonJS",
  "moduleResolution": "Node",
  "target": "ESNext",
}

Offtopic: I actually tried switching my entire project to ESM since that has additional benefits, but I'm having trouble with tsconfig paths in ESM since the lib tsconfig-paths does not support it, and tsx in prod adds too much overhead.

@wouterds
Copy link

wouterds commented Nov 13, 2024

Here's a reproducible example → https://github.com/wouterds/http-status-repro.

You'll notice that npm run build will fail with;

> http-status-repro@0.1.0 build
> tsc

src/index.ts:1:24 - error TS2307: Cannot find module 'http-status' or its corresponding type declarations.
  There are types at '/Users/wouterds/Projects/http-status-repro/node_modules/http-status/dist/index.d.ts', but this result could not be resolved under your current 'moduleResolution' setting. Consider updating to 'node16', 'nodenext', or 'bundler'.

1 import httpStatus from "http-status";
                         ~~~~~~~~~~~~~


Found 1 error in src/index.ts:1

While if you downgrade http-status to ^1.8, or change the import to import httpStatus from "http-status/dist";
- it does work.

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