← All tasks
javascriptunjs/consola #324Not a task: not reproduced

Unable to import package under `node16` module resolution mode

envgap__unjs__consola-324

01 / FAILURE SIGNATURE

As reported upstream

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:
Not a benchmark task.
  • In a clean container the reported failure did not reproduce, or the known fix did not make the project run.

02 / ENVIRONMENT RECIPE

Base commit
6734c473b0289b4f7fa9ba366842c66c6ce4bd33
Manifest
package.json
Reproduce
Awaiting issue-specific recipe
Run under trace
Awaiting a meaningful runtime command

03 / ORIGINAL ISSUE TEXT

unjs/consola #324 · read the original issue
### Environment



`consola@3.2.3`

`node@23.4.0`



### Reproduction



- https://github.com/bryan-hoang/repro-consola-import

- https://arethetypeswrong.github.io/?p=consola%403.2.3 (mainly the [Masquerading as ESM](https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseESM.md) 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:



```text

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](https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseESM.md) document and checking `attw --pack .`):



```diff

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



```sh

> 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.

```

Continue on GitHub ↗

04 / LABELS

Labels from the report text only; not yet run

No supported category has been assigned.

Label rules and the text that matched
[]