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

The CJS build of Vite's Node API is deprecated #350

Closed
EvgenyOrekhov opened this issue Nov 17, 2023 · 10 comments
Closed

The CJS build of Vite's Node API is deprecated #350

EvgenyOrekhov opened this issue Nov 17, 2023 · 10 comments
Labels
bug Something isn't working

Comments

@EvgenyOrekhov
Copy link

Started getting this error every time I run knip:

The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-dep

I use:

  • knip 2.41.3
  • Vitest 0.34.6
  • Node.js 18.18.2
@EvgenyOrekhov EvgenyOrekhov added the bug Something isn't working label Nov 17, 2023
@webpro
Copy link
Collaborator

webpro commented Nov 17, 2023

Is it just a warning, or an error? How can I reproduce it?

@EvgenyOrekhov
Copy link
Author

It's just a warning. I assume you need to install the latest version of vitest and add some basic vitest.config.ts.

@EvgenyOrekhov
Copy link
Author

Steps to reproduce:

mkdir knip-vitest
cd knip-vitest
npm i -D vitest knip

Then create file vitest.config.ts

/// <reference types="vitest" />
import { defineConfig } from 'vitest/config';
export default defineConfig({});

Then run npx knip > result.txt, and you should see the warning.

image

Note: you have to run npx knip > result.txt, and not just npx knip to see the warning, because npx knip clears the current output line.

@webpro
Copy link
Collaborator

webpro commented Nov 20, 2023

Thanks, gotcha.

The first thing that caught my eye is that you probably want to set "type": "module" in package.json, to nudge Node.js and other module resolvers in the right direction.

However, that won't be sufficient today, I think there's an issue in jiti. Knip uses jiti under the hood to load such config files. Hacked around a bit and didn't get it to work (not even when using the .mts extension), maybe fixed when they make the switch to ESM being the native import/loading mechanism in jiti v2.

To get rid of the warning today, you could switch to use the .mjs extension.

Note: you have to run npx knip > result.txt, and not just npx knip to see the warning, because npx knip clears the current output line.

Knip should not clear or overwrite that output (e.g. it does not do that on my macOS with Bash). Can I ask which shell you are using?

You can use --no-progress or -n so Knip won't rewrite lines or clear stdout.

@EvgenyOrekhov
Copy link
Author

Knip should not clear or overwrite that output (e.g. it does not do that on my macOS with Bash). Can I ask which shell you are using?

I use zsh on my macOS. I can see the warning gets shown for a split second. With --no-progress it stays on the screen.
BTW I believe the warning goes to stderr, not stdout.

@yekver
Copy link

yekver commented Nov 23, 2023

I have the same issue with '--no-progress' flag provided. In package.json set "type": "module".

@webpro
Copy link
Collaborator

webpro commented Nov 23, 2023

@yekver Those are two different things. The real issue here is about CJS vs ESM (with "type": "module").

The other thing is only about how Knip logs things to stdout (and stderr), and there seems to be a difference between Bash and Zsh. That's why we discuss the --no-progress flag.

@JoshuaKGoldberg
Copy link
Contributor

JoshuaKGoldberg commented Dec 7, 2023

FWIW I see the CJS notice in JoshuaKGoldberg/create-typescript-app#1082.

joshgoldberg ~/repos/create-typescript-app $ pnpm lint:knip

> [email protected] lint:knip /Users/josh/repos/create-typescript-app
> knip

Analyzing workspace ....
The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated
✂️  Excellent, Knip found no issues.
joshgoldberg ~/repos/create-typescript-app $ 

@webpro
Copy link
Collaborator

webpro commented Dec 7, 2023

Yeah, it's coming from jiti, waiting for them to go fully ESM :)

@webpro
Copy link
Collaborator

webpro commented Mar 21, 2024

See #565

@webpro webpro closed this as completed Mar 21, 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

No branches or pull requests

4 participants