node-http-status icon indicating copy to clipboard operation
node-http-status copied to clipboard

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

Open dschuessler opened this issue 1 year ago • 8 comments

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.

dschuessler avatar Nov 06 '24 12:11 dschuessler

same here

gregorio-gerardi avatar Nov 07 '24 13:11 gregorio-gerardi

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",
     }
    }
    

wdavidw avatar Nov 07 '24 16:11 wdavidw

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.

wouterds avatar Nov 13 '24 13:11 wouterds

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.

wdavidw avatar Nov 13 '24 14:11 wdavidw

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.

wouterds avatar Nov 13 '24 14:11 wouterds

With Node.js and without TypeScript ?

wdavidw avatar Nov 13 '24 14:11 wdavidw

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 avatar Nov 13 '24 14:11 wouterds

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

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

> [email protected] 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.

wouterds avatar Nov 13 '24 15:11 wouterds