Closed krutoo closed 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)
Can you create a Stackblitz repro so I can see this in action?
@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)
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. :)
@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
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.
@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?
@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
@nzakas it is definitely error in @humanwhocodes/env
, can you please fix that and add tests for TypeScript?
Hi, with TypeScript and
"module": "NodeNext"
i have compilation error:Can we fix that?
Other dependencies in my project such as
@sentry/*
,@opentelemetry/*
,prom-client
and other has no such error