aleclarson / vite-tsconfig-paths

Support for TypeScript's path mapping in Vite
MIT License
1.34k stars 50 forks source link

[ERROR] "vite-tsconfig-paths" resolved to an ESM file. ESM file cannot be loaded by `require` #155

Closed simplenotezy closed 3 months ago

simplenotezy commented 3 months ago

I am trying to install the Vitest VSCode Extension in my NestJS project, but upon installation I see the following error:

[ERROR] "vite-tsconfig-paths" resolved to an ESM file. ESM file cannot be loaded by `require`. See https://vitejs.dev/guide/troubleshooting.html#this-package-is-esm-only for more details. [plugin externalize-deps]

See full error log ``` [INFO 20:02:21] [v1.2.0] Vitest extension is activated because Vitest is installed or there is a Vite/Vitest config file in the workspace. [INFO 20:02:21] [API] Running Vitest v2.0.5 (dist/vitest.config.js) with Node.js: /Users/mf/.nvm/versions/node/v20.11.0/bin/node [INFO 20:02:21] [API] Running Vitest v2.0.5 (backend/vitest.e2e.config.mts) with Node.js: /Users/mf/.nvm/versions/node/v20.11.0/bin/node [INFO 20:02:21] [API] Running Vitest v2.0.5 (backend/vitest.config.mts) with Node.js: /Users/mf/.nvm/versions/node/v20.11.0/bin/node [INFO 20:02:21] [API] Running Vitest v2.0.5 (dist/vitest.config.mjs) with Node.js: /Users/mf/.nvm/versions/node/v20.11.0/bin/node [INFO 20:02:21] [API] Running Vitest v2.0.5 (dist/vitest.config.e2e.js) with Node.js: /Users/mf/.nvm/versions/node/v20.11.0/bin/node [INFO 20:02:21] [API] Running Vitest v2.0.5 (dist/vitest.e2e.config.js) with Node.js: /Users/mf/.nvm/versions/node/v20.11.0/bin/node [INFO 20:02:21] [API] Running Vitest v2.0.5 (dist/vitest.config.d.mts) with Node.js: /Users/mf/.nvm/versions/node/v20.11.0/bin/node [INFO 20:02:21] [API] Running Vitest v2.0.5 (dist/vitest.e2e.config.d.mts) with Node.js: /Users/mf/.nvm/versions/node/v20.11.0/bin/node [INFO 20:02:21] [API] Running Vitest v2.0.5 (dist/vitest.e2e.config.mjs) with Node.js: /Users/mf/.nvm/versions/node/v20.11.0/bin/node [INFO 20:02:21] [API] Running Vitest v2.0.5 (dist/vitest.config.e2e.mjs) with Node.js: /Users/mf/.nvm/versions/node/v20.11.0/bin/node [INFO 20:02:21] [API] Running Vitest v2.0.5 (dist/vitest.config.e2e.d.mts) with Node.js: /Users/mf/.nvm/versions/node/v20.11.0/bin/node [INFO 20:02:21] [API] Running Vitest v2.0.5 (dist/vitest.test.config.js) with Node.js: /Users/mf/.nvm/versions/node/v20.11.0/bin/node [Worker] ✘ [ERROR] "vite-tsconfig-paths" resolved to an ESM file. ESM file cannot be loaded by `require`. See https://vitejs.dev/guide/troubleshooting.html#this-package-is-esm-only for more details. [plugin externalize-deps] ../node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1225:27: 1225 │ let result = await callback({ ╵ ^ at file:///Users/mf/Projects/kite/backend/node_modules/.pnpm/vite@5.4.1_@types+node@20.14.13_terser@5.31.3/node_modules/vite/dist/node/chunks/dep-Cy9twKMn.js:66526:23 at requestCallbacks.on-resolve (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1225:28) at handleRequest (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:647:17) at handleIncomingPacket (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:672:7) at Socket.readFromStdout (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:600:7) at Socket.emit (node:events:518:28) at addChunk (node:internal/streams/readable:559:12) at readableAddChunkPushByteMode (node:internal/streams/readable:510:3) at Readable.push (node:internal/streams/readable:390:5) at Pipe.onStreamRead (node:internal/stream_base_commons:190:23) This error came from the "onResolve" callback registered here: ../node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1150:20: 1150 │ let promise = setup({ ╵ ^ at setup (file:///Users/mf/Projects/kite/backend/node_modules/.pnpm/vite@5.4.1_@types+node@20.14.13_terser@5.31.3/node_modules/vite/dist/node/chunks/dep-Cy9twKMn.js:66488:18) at handlePlugins (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1150:21) at buildOrContextImpl (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:873:5) at Object.buildOrContext (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:699:5) at /Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:2023:15 at new Promise () at Object.build (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:2022:25) at build (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1873:51) at bundleConfigFile (file:///Users/mf/Projects/kite/backend/node_modules/.pnpm/vite@5.4.1_@types+node@20.14.13_terser@5.31.3/node_modules/vite/dist/node/chunks/dep-Cy9twKMn.js:66443:24) The plugin "externalize-deps" was triggered by this import vitest.config.e2e.js:8:54: 8 │ ...tsconfig_paths_1 = __importDefault(require("vite-tsconfig-paths")); ╵ ~~~~~~~~~~~~~~~~~~~~~ failed to load config from /Users/mf/Projects/kite/backend/dist/vitest.config.e2e.js [Worker] ▲ [WARNING] The CommonJS "exports" variable is treated as a global variable in an ECMAScript module and may not work as expected [commonjs-variable-in-esm] vitest.config.mjs:9:0: 9 │ exports.default = (0, config_1.defineConfig)({ ╵ ~~~~~~~ This file is considered to be an ECMAScript module because the file name ends in ".mjs". [Worker] ▲ [WARNING] The CommonJS "exports" variable is treated as a global variable in an ECMAScript module and may not work as expected [commonjs-variable-in-esm] vitest.e2e.config.mjs:9:0: 9 │ exports.default = (0, config_1.defineConfig)({ ╵ ~~~~~~~ This file is considered to be an ECMAScript module because the file name ends in ".mjs". [Worker] failed to load config from /Users/mf/Projects/kite/backend/dist/vitest.config.mjs [Worker] failed to load config from /Users/mf/Projects/kite/backend/dist/vitest.e2e.config.mjs [Worker] ✘ [ERROR] "vite-tsconfig-paths" resolved to an ESM file. ESM file cannot be loaded by `require`. See https://vitejs.dev/guide/troubleshooting.html#this-package-is-esm-only for more details. [plugin externalize-deps] ../node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1225:27: 1225 │ let result = await callback({ ╵ ^ at file:///Users/mf/Projects/kite/backend/node_modules/.pnpm/vite@5.4.1_@types+node@20.14.13_terser@5.31.3/node_modules/vite/dist/node/chunks/dep-Cy9twKMn.js:66526:23 at requestCallbacks.on-resolve (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1225:28) at handleRequest (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:647:17) at handleIncomingPacket (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:672:7) at Socket.readFromStdout (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:600:7) at Socket.emit (node:events:518:28) at addChunk (node:internal/streams/readable:559:12) at readableAddChunkPushByteMode (node:internal/streams/readable:510:3) at Readable.push (node:internal/streams/readable:390:5) at Pipe.onStreamRead (node:internal/stream_base_commons:190:23) This error came from the "onResolve" callback registered here: ../node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1150:20: 1150 │ let promise = setup({ ╵ ^ at setup (file:///Users/mf/Projects/kite/backend/node_modules/.pnpm/vite@5.4.1_@types+node@20.14.13_terser@5.31.3/node_modules/vite/dist/node/chunks/dep-Cy9twKMn.js:66488:18) at handlePlugins (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1150:21) at buildOrContextImpl (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:873:5) at Object.buildOrContext (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:699:5) at /Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:2023:15 at new Promise () at Object.build (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:2022:25) at build (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1873:51) at bundleConfigFile (file:///Users/mf/Projects/kite/backend/node_modules/.pnpm/vite@5.4.1_@types+node@20.14.13_terser@5.31.3/node_modules/vite/dist/node/chunks/dep-Cy9twKMn.js:66443:24) The plugin "externalize-deps" was triggered by this import vitest.e2e.config.js:8:54: 8 │ ...tsconfig_paths_1 = __importDefault(require("vite-tsconfig-paths")); ╵ ~~~~~~~~~~~~~~~~~~~~~ failed to load config from /Users/mf/Projects/kite/backend/dist/vitest.e2e.config.js [Worker] 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. [Worker] failed to load config from /Users/mf/Projects/kite/backend/dist/vitest.e2e.config.d.mts [Worker] failed to load config from /Users/mf/Projects/kite/backend/dist/vitest.config.e2e.d.mts [Worker] ▲ [WARNING] The CommonJS "exports" variable is treated as a global variable in an ECMAScript module and may not work as expected [commonjs-variable-in-esm] vitest.config.e2e.mjs:9:0: 9 │ exports.default = (0, config_1.defineConfig)({ ╵ ~~~~~~~ This file is considered to be an ECMAScript module because the file name ends in ".mjs". failed to load config from /Users/mf/Projects/kite/backend/dist/vitest.config.e2e.mjs [INFO 20:02:22] [API] Vitest v2.0.5 (backend/vitest.config.mts) process 23908 created [Worker] failed to load config from /Users/mf/Projects/kite/backend/dist/vitest.config.d.mts [Worker] 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. [INFO 20:02:22] [API] Vitest v2.0.5 (dist/vitest.test.config.js) process 23917 created [INFO 20:02:22] [API] Vitest v2.0.5 (backend/vitest.e2e.config.mts) process 23907 created [INFO 20:02:22] [API] Vitest v2.0.5 (dist/vitest.config.js) process 23906 created [Error 20:02:22] [Error Error] Vitest failed to start: Error: Build failed with 1 error: ../node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1225:27: ERROR: [plugin: externalize-deps] "vite-tsconfig-paths" resolved to an ESM file. ESM file cannot be loaded by `require`. See https://vitejs.dev/guide/troubleshooting.html#this-package-is-esm-only for more details. at failureErrorWithLog (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1472:15) at /Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:945:25 at runOnEndCallbacks (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1315:45) at buildResponseToResult (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:943:7) at /Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:970:16 at responseCallbacks. (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:622:9) at handleIncomingPacket (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:677:12) at Socket.readFromStdout (/Users/mf/Projects/kite/backend/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:600:7) at Socket.emit (node:events:518:28) at addChunk (node:internal/streams/readable:559:12) Error: Vitest failed to start: ```

Any ideas what I am missing here? I use the .mts config file approach.

aleclarson commented 3 months ago

You have Vitest configs in your dist folder, compiled to CJS. Vitest is finding them and trying to use them, which leads to inadvertent require of this plugin. I recommend adding dist to your Vitest exclude patterns.

Closing as mistaken, since this is user error.

Git-I985 commented 2 months ago

You have Vitest configs in your dist folder, compiled to CJS. Vitest is finding them and trying to use them, which leads to inadvertent require of this plugin. I recommend adding dist to your Vitest exclude patterns.

Closing as mistaken, since this is user error.

@aleclarson btw didnt help me

exclude: ['**/node_modules/**', '**/dist/**'],
✘ [ERROR] "vite-tsconfig-paths" resolved to an ESM file. ESM file cannot be loaded by `require`
ehyland commented 2 months ago

@Git-I985 I just hit this too, solution for me was to set "type": "module" in my package.json

Git-I985 commented 2 months ago

@Git-I985 I just hit this too, solution for me was to set "type": "module" in my package.json

its a kinda major change for big old projects :))

shawnrivers commented 1 month ago

Renaming my vitest.config.ts to vitest.config.mts fixed the error for me.

This solution was documented in https://vitejs.dev/guide/troubleshooting.html#this-package-is-esm-only (which is in the original error message).