cypress icon indicating copy to clipboard operation
cypress copied to clipboard

[CT] Fix Vite 5 CJS Deprecation Warning and Prepare for Vite 6

Open haoqunjiang opened this issue 2 years ago • 7 comments

Current behavior

When using Cypress Component Testing with Vite 5, if vite.config.ts is used, a CJS Node API deprecation warning will always appear, despite "module": "NodeNext" or "module": "ESNext" being specified in the root tsconfig.json and "type": "module" being present in package.json.

Desired behavior

Should not show the CJS warning because vite.config.ts should be treated as an ES module when type: "module" presents.

Test code to reproduce

git clone https://github.com/sodatea/vite-5-cypress-reproduction
cd vite-5-cypress-reproduction
pnpm i
pnpm test:unit

The following warning will be printed:

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

and

We detected that you have versions of dependencies that are not officially supported:

 - `vite`. Expected ^2.0.0 || ^3.0.0 || ^4.0.0, found 5.0.0.

If you're experiencing problems, downgrade dependencies and restart Cypress.

The latter issue has already been tracked at https://github.com/cypress-io/cypress/issues/28347

Cypress Version

13.5.1

Node version

v18.18.2

Operating System

macOS 14.1

Debug Logs

No response

Other

I guess this is because of the limitation of ts-node that Cypress uses underlyingly to load the configuration files: https://github.com/TypeStrong/ts-node/issues/1791#issuecomment-1149754228

Considering Vite 6 will drop CJS Node API, I think Cypress needs to either work around this limitation or move away from ts-node as early as possible.

haoqunjiang avatar Nov 21 '23 08:11 haoqunjiang

IMO, getting rid of ts-node may be a better option here. Because there are other well-known issues of this tool. For example, it doesn't support solution-style tsconfigs, forcing us to specify a compilerOptions.module option in the root tsconfig.json, even though that field doesn't mean anything for any other tool. This is not a good DX.

haoqunjiang avatar Nov 21 '23 08:11 haoqunjiang

The warning

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

links also to

  • https://github.com/vitejs/vite/discussions/13928

MikeMcC399 avatar Dec 09 '23 13:12 MikeMcC399

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

cypress-app-bot avatar Jun 07 '24 01:06 cypress-app-bot

  • Related to issue https://github.com/cypress-io/cypress/issues/29557

MikeMcC399 avatar Jun 07 '24 05:06 MikeMcC399

currently caused by require.resolve.

// "vite-dev-server" is bundled in the binary, so we need to require.resolve "vite"
// from root of the active project since we don't bundle vite internally but rather
// use the version the user has installed
export function getVite (config: ViteDevServerConfig): Vite {
  try {
    const viteImportPath = require.resolve('vite', { paths: [config.cypressConfig.projectRoot] })

    debug('resolved viteImportPath as %s', viteImportPath)

    return require(viteImportPath)
  } catch (err) {
    throw new Error(`Could not find "vite" in your project's dependencies. Please install "vite" to fix this error.\n\n${err}`)
  }
}

gweesin avatar Jul 10 '24 01:07 gweesin

Any way to resolve this?

joshbuker avatar Jul 17 '24 01:07 joshbuker

It looks like vite 6 has not yet removed the vite cjs API yet, so this does not block vite 6 support.

For Cypress to get rid of using the cjs API requires some involved steps

  • Changing the require.resolve and require in getVite to use dynamic import syntax.
  • Changing the way @cypress/vite-dev-server is bundled, changing the target in the tsconfig

from

"module": "commonjs",
"moduleResolution": "node",

to

"module": "ESNext",
"moduleResolution": "node",

This will make the package compile down to ESM.

We would also need to make sure moving the plugin manager over to ESM could still work with end user cjs plugins.

Either way, the work here is fairly involved.

AtofStryker avatar Nov 26 '24 21:11 AtofStryker

closing as complete with https://github.com/cypress-io/cypress/pull/32001

AtofStryker avatar Jul 17 '25 16:07 AtofStryker

Released in 15.0.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to Cypress v15.0.0, please open a new issue.

cypress-bot[bot] avatar Aug 20 '25 17:08 cypress-bot[bot]