Home > Net >  My JS npm package causes 'Could not find a declaration file for module' error?
My JS npm package causes 'Could not find a declaration file for module' error?

Time:05-19

I published an npm package that is JavaScript. It works fine, but there are always three dots in front of the name when you import it into another app and an error message that says:

Could not find a declaration file for module 'my-npm-package'

How do i create a declaration file? The package is not TypeScript. I've never used TypeScript before.

Here is how may file tree looks:

index.js
package.json
README.md

CodePudding user response:

you can use this command to generate a index.d.ts file:

tsc --declaration --allowJs --emitDeclarationOnly index.js

NOTE: if you don't have typescript installed in your system, you need it for this command to work:

npm install -g typescript

also you can add a types section to your package.json file to point to your declarations file.

{
...
"main": "index.js",
"types": "index.d.ts",
...
}
  • Related