vite-aliases: Error: Dynamic require of "path" is not supported

Hi, great work on this!

After updating from 0.8.7 to 0.9.1 i get the following error when building:

failed to load config from vite.config.js
error when starting dev server:
Error: Dynamic require of "path" is not supported
    at file:///src/webapp/node_modules/.pnpm/vite-aliases@0.9.1_sass@1.49.9/node_modules/vite-aliases/dist/index.mjs:30:9
    at node_modules/fast-glob/out/utils/path.js (file:///src/webapp/node_modules/.pnpm/vite-aliases@0.9.1_sass@1.49.9/node_modules/vite-aliases/dist/index.mjs:131:16)
    at __require2 (file:///src/webapp/node_modules/.pnpm/vite-aliases@0.9.1_sass@1.49.9/node_modules/vite-aliases/dist/index.mjs:36:50)
    at node_modules/fast-glob/out/utils/index.js (file:///src/webapp/node_modules/.pnpm/vite-aliases@0.9.1_sass@1.49.9/node_modules/vite-aliases/dist/index.mjs:3235:16)
    at __require2 (file:///src/webapp/node_modules/.pnpm/vite-aliases@0.9.1_sass@1.49.9/node_modules/vite-aliases/dist/index.mjs:36:50)
    at node_modules/fast-glob/out/managers/tasks.js (file:///src/webapp/node_modules/.pnpm/vite-aliases@0.9.1_sass@1.49.9/node_modules/vite-aliases/dist/index.mjs:3253:17)
    at __require2 (file:///src/webapp/node_modules/.pnpm/vite-aliases@0.9.1_sass@1.49.9/node_modules/vite-aliases/dist/index.mjs:36:50)
    at node_modules/fast-glob/out/index.js (file:///src/webapp/node_modules/.pnpm/vite-aliases@0.9.1_sass@1.49.9/node_modules/vite-aliases/dist/index.mjs:5167:23)
    at __require2 (file:///src/webapp/node_modules/.pnpm/vite-aliases@0.9.1_sass@1.49.9/node_modules/vite-aliases/dist/index.mjs:36:50)
    at file:///src/webapp/node_modules/.pnpm/vite-aliases@0.9.1_sass@1.49.9/node_modules/vite-aliases/dist/index.mjs:5240:32
 ELIFECYCLE  Command failed with exit code 1.

working: working.zip repro: repro.zip

using versions vite: 2.8.6 node: v17.1.0 pnpm: 6.23.2 npm: 8.1.2

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 25 (11 by maintainers)

Most upvoted comments

After further troubleshooting i managed to build with 0.9.1 by removing type: "module" from package.json. Would it be worth looking into why type: "module" worked with 0.8.7 but not with 0.9.1?.

new-repro.zip

In my case I just change const path = require('path') to import path from 'path' and works.

Add the below code in the ‘vite.config.ts’ or ‘vite.config.js’ file:

import { resolve } from 'path';

export default defineConfig({
  resolve: {
    alias: {
      '@': resolve(__dirname, './src')
    },
  },
  plugins: [vue()],
})

@Subwaytime I had this same issue recently with new Vite project and removing “type”: “module” fixed the issue.

if package.json "type": "module", then vite.config.js import path from 'path';

fix is coming with the newest release!

quick note: due to covid i wasnt able to work on this again, but now i can finally continue!

also, i will bundle this with the upcoming major changes from vite 3!

esm is technically still experimental on nodejs 16+, so you might run into troubles with this anyways. normally its not recommended, i will see if i can get vite-aliases to support it, or atleast find a work around if fast-glob is causing the issue.