Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to import package under node16 module resolution mode #324

Closed
bryan-hoang opened this issue Dec 19, 2024 · 0 comments · Fixed by #331
Closed

Unable to import package under node16 module resolution mode #324

bryan-hoang opened this issue Dec 19, 2024 · 0 comments · Fixed by #331
Labels
bug Something isn't working

Comments

@bryan-hoang
Copy link

bryan-hoang commented Dec 19, 2024

Environment

consola@3.2.3
node@23.4.0

Reproduction

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.
@bryan-hoang bryan-hoang added the bug Something isn't working label Dec 19, 2024
@pi0 pi0 closed this as completed in 18bc852 Dec 26, 2024
@pi0 pi0 reopened this Dec 26, 2024
pi0 added a commit that referenced this issue Dec 27, 2024
@pi0 pi0 closed this as completed in #331 Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants