Julien-R44 / vite-plugin-validate-env

✅ Vite plugin for validating your environment variables
MIT License
164 stars 6 forks source link

Compilation fails after upgrading to Vite 5 #21

Closed Vilican closed 6 months ago

Vilican commented 9 months ago

Full error message:

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.
failed to load config from C:\Users\<user>\PhpstormProjects\<project>\vite.config.ts
error when starting dev server:
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\<user>\PhpstormProjects\<project>\node_modules\@poppinss\cliui\build\index.js from C:\Users\<user>\PhpstormProjects\<project>\node_modules\@julr\vite-plugin-validate-env\dist\index.cjs not supported.
Instead change the require of index.js in C:\Users\<user>\PhpstormProjects\<project>\node_modules\@julr\vite-plugin-validate-env\dist\index.cjs to a dynamic import() which is available in all CommonJS modules.
    at _require.extensions.<computed> [as .js] (file:///C:/Users/<user>/PhpstormProjects/<project>/node_modules/vite/dist/node/chunks/dep-4RECYSE1.js:67481:17)
    at Object.<anonymous> (C:\Users\<user>\PhpstormProjects\<project>\node_modules\@julr\vite-plugin-validate-env\dist\index.cjs:7:15)
    at _require.extensions.<computed> [as .js] (file:///C:/Users/<user>/PhpstormProjects/<project>/node_modules/vite/dist/node/chunks/dep-4RECYSE1.js:67481:17)

Process finished with exit code 1
Julien-R44 commented 9 months ago

Is your project in ESM? Please read the link Vite gives you just before the error message: https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated

Your package.json should have "type": "module".

Vilican commented 9 months ago

Yeah, thanks for the quick fix. It still gives a warning though:

Trace: 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.
    at warnCjsUsage (C:\Users\<user>\PhpstormProjects\<project>\node_modules\vite\index.cjs:32:3)
    at Object.<anonymous> (C:\Users\<user>\PhpstormProjects\<project>\node_modules\vite\index.cjs:3:1)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)
    at Object.<anonymous> (C:\Users\<user>\PhpstormProjects\<project>\node_modules\@julr\vite-plugin-validate-env\dist\index.cjs:6:14)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)
    at evalModule (C:\Users\<user>\PhpstormProjects\<project>\node_modules\jiti\dist\jiti.js:1:255612)
    at jiti (C:\Users\<user>\PhpstormProjects\<project>\node_modules\jiti\dist\jiti.js:1:254371)
    at C:/Users/<user>/PhpstormProjects/<project>/env.ts:3:30
    at evalModule (C:\Users\<user>\PhpstormProjects\<project>\node_modules\jiti\dist\jiti.js:1:256443)
    at jiti (C:\Users\<user>\PhpstormProjects\<project>\node_modules\jiti\dist\jiti.js:1:254371)
    at loadConfigFile (file:///C:/Users/<user>/PhpstormProjects/<project>/node_modules/unconfig/dist/index.mjs:153:13)
    at async Object.load (file:///C:/Users/<user>/PhpstormProjects/<project>/node_modules/unconfig/dist/index.mjs:76:24)
    at async loadConfig (file:///C:/Users/<user>/PhpstormProjects/<project>/node_modules/@julr/vite-plugin-validate-env/dist/index.mjs:81:18)
    at async validateEnv (file:///C:/Users/<user>/PhpstormProjects/<project>/node_modules/@julr/vite-plugin-validate-env/dist/index.mjs:115:15)
    at async runConfigHook (file:///C:/Users/<user>/PhpstormProjects/<project>/node_modules/vite/dist/node/chunks/dep-4RECYSE1.js:67497:25)
    at async resolveConfig (file:///C:/Users/<user>/PhpstormProjects/<project>/node_modules/vite/dist/node/chunks/dep-4RECYSE1.js:66950:14)
    at async _createServer (file:///C:/Users/<user>/PhpstormProjects/<project>/node_modules/vite/dist/node/chunks/dep-4RECYSE1.js:59489:20)
    at async CAC.<anonymous> (file:///C:/Users/<user>/PhpstormProjects/<project>/node_modules/vite/dist/node/cli.js:764:24)
paolostyle commented 9 months ago

I have the same issue, I did not have the warning before using this plugin, seems like it's something in that jiti dependency, though it's a bit ironic considering what that package does 🤷 Adding the stack trace as well:

$ VITE_CJS_TRACE=true npx vite build
Trace: 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.
    at warnCjsUsage (/home/<project>/node_modules/vite/index.cjs:32:3)
    at Object.<anonymous> (/home/<project>/node_modules/vite/index.cjs:3:1)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Object.<anonymous> (/home/<project>/node_modules/@julr/vite-plugin-validate-env/dist/index.cjs:6:14)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:119:18)
    at evalModule (/home/<project>/node_modules/unconfig/node_modules/jiti/dist/jiti.js:1:255612)
    at jiti (/home/<project>/node_modules/unconfig/node_modules/jiti/dist/jiti.js:1:254371)
    at /home/<project>/env.mts:1:192
    at evalModule (/home/<project>/node_modules/unconfig/node_modules/jiti/dist/jiti.js:1:256443)
    at jiti (/home/<project>/node_modules/unconfig/node_modules/jiti/dist/jiti.js:1:254371)
    at loadConfigFile (file:///home/<project>/node_modules/unconfig/dist/index.mjs:153:13)
    at async Object.load (file:///home/<project>/node_modules/unconfig/dist/index.mjs:76:24)
    at async loadConfig (file:///home/<project>/node_modules/@julr/vite-plugin-validate-env/dist/index.mjs:81:18)
    at async validateEnv (file:///home/<project>/node_modules/@julr/vite-plugin-validate-env/dist/index.mjs:115:15)
    at async runConfigHook (file:///home/<project>/node_modules/vite/dist/node/chunks/dep-4RECYSE1.js:67497:25)
    at async resolveConfig (file:///home/<project>/node_modules/vite/dist/node/chunks/dep-4RECYSE1.js:66950:14)
    at async build (file:///home/<project>/node_modules/vite/dist/node/chunks/dep-4RECYSE1.js:66093:20)
    at async CAC.<anonymous> (file:///home/<project>/node_modules/vite/dist/node/cli.js:845:9)
gunta commented 9 months ago

Reproducing here too.

gunta commented 9 months ago

Can confirm it breaks when trying to load from a separate file (env.ts), but it works fine when is in the vite.config.mts file

Julien-R44 commented 9 months ago

Thanks guys. So that means it's probably coming from unconfig. I'll investigate before the end of the week

ujwal-setlur commented 8 months ago

Hi @Julien-R44, any update on this? I am getting this warning as well

wowblvck commented 7 months ago

Hi @Julien-R44. I have a same warning too. Is there any solution?

Julien-R44 commented 7 months ago

haven't had time to investigate yet. happy to review a PR for this

Julien-R44 commented 6 months ago

Fixed with version 1.1.1