humanwhocodes / env

A utility for verifying environment variables are present
BSD 3-Clause "New" or "Revised" License
377 stars 16 forks source link

Could not find a declaration file for module '@humanwhocodes/env' #133

Closed krutoo closed 7 months ago

krutoo commented 7 months ago

Hi, with TypeScript and "module": "NodeNext" i have compilation error:

Could not find a declaration file for module '@humanwhocodes/env'

Can we fix that?

Other dependencies in my project such as @sentry/*, @opentelemetry/*, prom-client and other has no such error

krutoo commented 7 months ago

It worked after I copied file env.d.ts and renamed it to env.cjs.d.ts

I worked a little with dual packages and noticed that the most workable case is when there are two folders, one for ESM and one for commonJS. And most importantly, both folders should contain their own type declarations files

(this is how dnt works)

nzakas commented 7 months ago

Can you create a Stackblitz repro so I can see this in action?

krutoo commented 7 months ago

@nzakas Sure, here is an repro: https://stackblitz.com/edit/typescript-node-hpq7zy?file=src%2Findex.ts

Noticed that I'm using version 2.0.2

But with the latest version another error appears:

The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@humanwhocodes/env")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to 'stackblitz:/package.json'.(1479)
nzakas commented 7 months ago

You're getting that error because index.js is a CommonJS file, which does not support import. You either need to change the extension to .mts or include "type": "module" in your package.json file...which is what the error message says. :)

krutoo commented 7 months ago

@nzakas But why it is not happening with all other packages?

I thought this package is dual and it should works with esm and cjs and also with typescript

nzakas commented 7 months ago

The error isn't about the package, it's about the file you're writing. I can't explain any more clearly than the error message already does.

krutoo commented 7 months ago

@nzakas I updated the example and added clean .cjs and .mjs files: https://stackblitz.com/edit/typescript-node-hpq7zy?file=src%2Findex.ts

Both of this files is working with @humanwhocodes/env, problem is only with .ts and type checking

I also added using another dual library in all three files.

Both of .cjs and .mjs files working

.ts file not working only because of @humanwhocodes/env and works with other dual library

Can you help me to understand: why should I add a module type to the package.json or change the extension to .mts if after compilation I get require() calls that work with this library?

krutoo commented 7 months ago

@nzakas Error directly says:

the referenced file is an ECMAScript module and cannot be imported with 'require'

But referenced file is CommonJS module in this case, as you can see when run node index.cjs

krutoo commented 7 months ago

@nzakas it is definitely error in @humanwhocodes/env, can you please fix that and add tests for TypeScript?