-
Notifications
You must be signed in to change notification settings - Fork 206
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Environment
[email protected]
[email protected]
Reproduction
- https://github.com/bryan-hoang/repro-consola-import
- https://arethetypeswrong.github.io/?p=consola%403.2.3 (mainly the Masquerading as ESM issue)
Describe the bug
While in a Typescript project with the "moduleResolution": "node16" option set, trying to import consola or a subpath (e.g., consola/utils) causes compilation/type checking to fail with the following error:
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("consola")' call instead.
To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to '/home/bryan/src/github.com/bryan-hoang/repro-consola-import/package.json'.
1 import consola from 'consola';
The following patch resolves the issue so that the consumer isn't forced to apply the "type": "module" suggestion or change "moduleResolution": "node16" (based on the Masquerading as ESM document and checking attw --pack .):
diff --git i/package.json w/package.json
index 8b99f49..810b5a8 100644
--- i/package.json
+++ w/package.json
@@ -22,40 +22,36 @@
"exports": {
".": {
"node": {
- "types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
- "require": "./lib/index.cjs"
+ "require": {
+ "types": "./dist/index.d.cts",
+ "default": "./lib/index.cjs"
+ }
},
"default": {
- "types": "./dist/browser.d.ts",
"import": "./dist/browser.mjs",
"require": "./dist/browser.cjs"
}
},
"./browser": {
- "types": "./dist/browser.d.ts",
"import": "./dist/browser.mjs",
"require": "./dist/browser.cjs"
},
"./basic": {
"node": {
- "types": "./dist/basic.d.ts",
"import": "./dist/basic.mjs",
"require": "./dist/basic.cjs"
},
"default": {
- "types": "./dist/browser.d.ts",
"import": "./dist/browser.mjs",
"require": "./dist/browser.cjs"
}
},
"./core": {
- "types": "./dist/core.d.ts",
"import": "./dist/core.mjs",
"require": "./dist/core.cjs"
},
"./utils": {
- "types": "./dist/utils.d.ts",
"import": "./dist/utils.mjs",
"require": "./dist/utils.cjs"
}If that works, I'd love to submit a PR for the patch!
Additional context
I encountered this issue in a discord bot project scaffolded with npx @sapphire/cli new.
Logs
> repro-consola-import@ repro /home/bryan/src/github.com/bryan-hoang/repro-consola-import
> tsc --noEmit
index.ts:1:21 - error TS1479: 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("consola")' call instead.
To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to '/home/bryan/src/github.com/bryan-hoang/repro-consola-import/package.json'.
1 import consola from 'consola';
~~~~~~~~~
index.ts:2:24 - error TS1479: 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("consola/utils")' call instead.
To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to '/home/bryan/src/github.com/bryan-hoang/repro-consola-import/package.json'.
2 import { colors } from 'consola/utils';
~~~~~~~~~~~~~~~
Found 2 errors in the same file, starting at: index.ts:1
ELIFECYCLE Command failed with exit code 2.Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working