Changing moduleResolution from Node to Node16 throws error
See original GitHub issueDescribe 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
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created a year ago
- Reactions:4
- Comments:5
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
That’s because TypeScript supports the
exports
-field when using"moduleResolution": "NodeNext"
orNode16
.@vitejs/plugin-vue
(or@vitejs/plugin-react
) in your case declaresexports
like this:When TypeScript resolves this, it reaches the
./dist/index.d.ts
. This is not anmts
, norcts
, so thepackage.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 itsexport { vuePlugin as default, /*...*/ }
. But when using CommonJS, exporting something asdefault
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:
Or when they are named exactly like this, dropping
types
would work, too ({"import": "./dist/index.mjs", "require": "./dist/index.cjs"}
).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
:Importing it with Node.js 18:
then prints:
Which is exactly what TypeScript 4.9.3 with
moduleResolution: Node16
infers: