Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Unable to import package under node16 module resolution mode #324

@bryan-hoang

Description

@bryan-hoang

Environment

[email protected]
[email protected]

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions