question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Changing moduleResolution from Node to Node16 throws error

See original GitHub issue

Describe the bug

Trying the Vite React setup on StackBlitz & my local setup, changing the moduleResolution to Node16 throws this error but the server starts and shows the Vite + React page, but it is kinda annoying to have this red highlight on the IDE

Reproduction

https://stackblitz.com/edit/vitejs-vite-qqmddf?file=vite.config.ts&terminal=dev

Steps to reproduce

Change moduleResolution of tsconfig to Node16

System Info

StackBlitz

Used Package Manager

pnpm

Logs

image

Validations

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:4
  • Comments:5

github_iconTop GitHub Comments

7reactions
michael42commented, Oct 17, 2022

That’s because TypeScript supports the exports-field when using "moduleResolution": "NodeNext" or Node16.

@vitejs/plugin-vue (or @vitejs/plugin-react) in your case declares exports like this:

{
  "name": "@vitejs/plugin-vue",
  "version": "3.1.0",
  // ...
  "exports": {
    ".": {
      "types": "./dist/index.d.ts",
      "import": "./dist/index.mjs",
      "require": "./dist/index.cjs"
    }
  }
  // ...
}

When TypeScript resolves this, it reaches the ./dist/index.d.ts. This is not an mts, nor cts, so the package.json-module-field determines the module system. Since that’s not set, it defaults to CommonJS.

And finally TypeScript, loads the index.d.ts with its export { vuePlugin as default, /*...*/ }. But when using CommonJS, exporting something as default does not cause it to be the default export when importing from ESM. module.exports.default = 42 is default-importable, but it’s { default: 42 } at run-time.

As far as I know, the vite plugins would need to provide separate type definitions for CommonJS and ESM to fix that mismatch, like this:

  "exports": {
    ".": {
      "import": {
        "types": "./dist/index.d.mts",
        "default": "./dist/index.mjs"
      },
      "require": {
        "types": "./dist/index.d.cts",
        "default": "./dist/index.cjs"
      }
    }
  }

Or when they are named exactly like this, dropping types would work, too ({"import": "./dist/index.mjs", "require": "./dist/index.cjs"}).

0reactions
michael42commented, Nov 28, 2022

Just looked at it and I’m not sure if that’s the same issue or if I understand Ryan Cavanaugh properly. I do, however, think that TypeScript is correct in that case and there’s a bug in the type definitions.

Consider this example, that defines a CommonJS export named default:

// y.cjs:
exports.default = 1

// y.d.cts:
declare const y = 1
export default y

Importing it with Node.js 18:

import y1 from "./y.cjs"
import * as y2 from "./y.cjs"
import {default as y3} from "./y.cjs"

console.log("y1", y1)
console.log("y2", y2)
console.log("y3", y3)

then prints:

y1 { default: 1 }
y2 [Module: null prototype] { default: { default: 1 } }
y3 { default: 1 }

Which is exactly what TypeScript 4.9.3 with moduleResolution: Node16 infers:

y1 satisfies { default: 1 }
y2 satisfies { default: { default: 1 } }
y3 satisfies { default: 1 }
Read more comments on GitHub >

github_iconTop Results From Across the Web

0 - Stack Overflow
I'm getting this error while compiling ts project. ... Did you mean to set the 'moduleResolution' option to 'node', or to add aliases...
Read more >
Documentation - Module Resolution - TypeScript
There are two possible module resolution strategies: Node and Classic. You can use the moduleResolution option to specify the module resolution strategy.
Read more >
Troubleshooting | ts-node - TypeStrong · GitHub
This error is thrown by node when a module is require() d, but node believes it should execute as native ESM. This can...
Read more >
TypeScript errors and how to fix them
Try to replace your enum declaration with a type: ... import readline from 'node:readline'; ... We need a statement to throw an error...
Read more >
Using ES modules in Node.js - LogRocket Blog
json file or the other ways specified above, Node throws the error shown below: (node:2844) Warning: To ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found