"Cannot find module 'http-status' or its corresponding type declarations." after migrating to 2.0.0
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
- Clone https://github.com/dschuessler/http-status-import-bug
- Run
npm run build
Additional context
I consider this a bug since the README claims that, for ESM users, the import remains the same.
same here
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", } }
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.
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.
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.
With Node.js and without TypeScript ?
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.
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.