vite-aliases icon indicating copy to clipboard operation
vite-aliases copied to clipboard

Error: Dynamic require of "path" is not supported

Open ivarlovlie opened this issue 3 years ago • 17 comments

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/[email protected][email protected]/node_modules/vite-aliases/dist/index.mjs:30:9
    at node_modules/fast-glob/out/utils/path.js (file:///src/webapp/node_modules/.pnpm/[email protected][email protected]/node_modules/vite-aliases/dist/index.mjs:131:16)
    at __require2 (file:///src/webapp/node_modules/.pnpm/[email protected][email protected]/node_modules/vite-aliases/dist/index.mjs:36:50)
    at node_modules/fast-glob/out/utils/index.js (file:///src/webapp/node_modules/.pnpm/[email protected][email protected]/node_modules/vite-aliases/dist/index.mjs:3235:16)
    at __require2 (file:///src/webapp/node_modules/.pnpm/[email protected][email protected]/node_modules/vite-aliases/dist/index.mjs:36:50)
    at node_modules/fast-glob/out/managers/tasks.js (file:///src/webapp/node_modules/.pnpm/[email protected][email protected]/node_modules/vite-aliases/dist/index.mjs:3253:17)
    at __require2 (file:///src/webapp/node_modules/.pnpm/[email protected][email protected]/node_modules/vite-aliases/dist/index.mjs:36:50)
    at node_modules/fast-glob/out/index.js (file:///src/webapp/node_modules/.pnpm/[email protected][email protected]/node_modules/vite-aliases/dist/index.mjs:5167:23)
    at __require2 (file:///src/webapp/node_modules/.pnpm/[email protected][email protected]/node_modules/vite-aliases/dist/index.mjs:36:50)
    at file:///src/webapp/node_modules/.pnpm/[email protected][email protected]/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

ivarlovlie avatar Mar 09 '22 23:03 ivarlovlie

Hey thanks for reporting this! Will take a look later today what could cause this 🤔

Subwaytime avatar Mar 10 '22 11:03 Subwaytime

@ivarlovlie Would you be able to provide a reproduction? Also it would be good to know what node version you are using. Ive tried to replicate the issue with node 16, npm 8 and vite 2.8.6 and dont run into the same Issue!

Subwaytime avatar Mar 10 '22 12:03 Subwaytime

repro: repro.zip

I am able to build the repro project when i'm on 0.8.7 of vite-aliases, and unable on 0.9.1.

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

ivarlovlie avatar Mar 10 '22 13:03 ivarlovlie

I did a test reproduction in this git repo here: https://github.com/Subwaytime/vite-aliases-test - edit: not needed anymore

It works flawlessly for me, even tried pnpm.

used pnpm: 6.32.3 (latest)

Cant seem to replicate your Issue. What operating system do you use?

Subwaytime avatar Mar 10 '22 16:03 Subwaytime

It looks to me this could be an issue with node 17 compared to node 16. So if you could try using 16 for a test that would be great.

Subwaytime avatar Mar 10 '22 17:03 Subwaytime

I am using Ubuntu 20.04 Tried with node 16.14.0 but still see the same behaviour using vite-aliases 0.9.1.

Cant seem to replicate your Issue

You're not able to reproduce on the project inside of repro.zip?

ivarlovlie avatar Mar 10 '22 19:03 ivarlovlie

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

ivarlovlie avatar Mar 10 '22 19:03 ivarlovlie

Yeah so the type in package forces the build/dev to be in esm rather then cjs. I am checking now whether this is due to vite-aliases or a change in fast-glob, as this is reporting the error.

Subwaytime avatar Mar 10 '22 19:03 Subwaytime

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.

Subwaytime avatar Mar 10 '22 19:03 Subwaytime

Just wanted to say I was getting the same error. Tried going down to 16.14.2 of node and that didn't help. Was on 9.1 but upgraded to 9.2 of vite-aliases and it built. Is this then resolved? Below was the error I was getting if it were to be any help if it's not resolved.

error when starting dev server: Error: Dynamic require of "path" is not supported at file:///Users/jessecoble/Repositories/PLAYGROUND/SvelteTemplate/SvelteTemplate/node_modules/vite-aliases/dist/index.mjs:30:9 at node_modules/fast-glob/out/utils/path.js (file:///Users/jessecoble/Repositories/PLAYGROUND/SvelteTemplate/SvelteTemplate/node_modules/vite-aliases/dist/index.mjs:131:16) at __require2 (file:///Users/jessecoble/Repositories/PLAYGROUND/SvelteTemplate/SvelteTemplate/node_modules/vite-aliases/dist/index.mjs:36:50) at node_modules/fast-glob/out/utils/index.js (file:///Users/jessecoble/Repositories/PLAYGROUND/SvelteTemplate/SvelteTemplate/node_modules/vite-aliases/dist/index.mjs:3235:16) at __require2 (file:///Users/jessecoble/Repositories/PLAYGROUND/SvelteTemplate/SvelteTemplate/node_modules/vite-aliases/dist/index.mjs:36:50) at node_modules/fast-glob/out/managers/tasks.js (file:///Users/jessecoble/Repositories/PLAYGROUND/SvelteTemplate/SvelteTemplate/node_modules/vite-aliases/dist/index.mjs:3253:17) at __require2 (file:///Users/jessecoble/Repositories/PLAYGROUND/SvelteTemplate/SvelteTemplate/node_modules/vite-aliases/dist/index.mjs:36:50) at node_modules/fast-glob/out/index.js (file:///Users/jessecoble/Repositories/PLAYGROUND/SvelteTemplate/SvelteTemplate/node_modules/vite-aliases/dist/index.mjs:5167:23) at __require2 (file:///Users/jessecoble/Repositories/PLAYGROUND/SvelteTemplate/SvelteTemplate/node_modules/vite-aliases/dist/index.mjs:36:50) at file:///Users/jessecoble/Repositories/PLAYGROUND/SvelteTemplate/SvelteTemplate/node_modules/vite-aliases/dist/index.mjs:5240:32

jcoble avatar Apr 08 '22 10:04 jcoble

I'm sorry, i actually downgraded to 8.6 of vite-aliases and that worked. I thought that's what I did but my package.json was showing 9.2 for some reason. I stopped and tried to restart the dev server and got the error again. Tried downgrading to 8.6 again, this time it "took" and showed as 8.6 in package.json and was then able to start the dev server. Sorry for the confusion.

jcoble avatar Apr 08 '22 10:04 jcoble

Hmm thats interesting, are you using "type":"module" in your package.json too? or without it?

Subwaytime avatar Apr 08 '22 20:04 Subwaytime

Ya. I tried taking it out and just got a bunch more errors. I don't know a whole lot about bundlers, config files etc. Just been learning more about them, especially Vite. There's something special about it that just really intrigues me. I guess its just the pure speed of it and how it can build 100x faster than other bundlers. Anyways, if you need anymore info let me know. Be happy to help.

jcoble avatar Apr 09 '22 10:04 jcoble

vite and vite-aliases should both run without "type":"module". If you dont have any other plugins installed that need this configuration specifically!

Ive been working on th ESM support, but it will still take some time, as other Bugfixes need to get implemented first!

sorry for the late response

Subwaytime avatar Apr 12 '22 10:04 Subwaytime

+1 this issue - node v16.14.2, with type=module . v0.8.6 works Thx for the project though!

Tommertom avatar Jun 09 '22 19:06 Tommertom

fix is coming with the newest release!

Subwaytime avatar Jun 19 '22 10:06 Subwaytime

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!

Subwaytime avatar Jul 06 '22 10:07 Subwaytime

https://vitejs.dev/guide/build.html#library-mode

image

Aurorxa avatar Sep 01 '22 02:09 Aurorxa

@Aurorxa thanks for that link, i have already updated these according to the docs, there are just some other bugs that currently interfere with a release

Subwaytime avatar Sep 04 '22 14:09 Subwaytime

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

MattMarquise avatar Oct 19 '22 18:10 MattMarquise

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()],
})

binulad avatar Feb 09 '23 11:02 binulad

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

MoySantanaV avatar Mar 22 '23 16:03 MoySantanaV

@Subwaytime Is there still a planned fix so "type": "module" doesn't have to be removed from package.json?

izetter avatar Aug 19 '23 06:08 izetter

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

thuyduongbka avatar Nov 07 '23 02:11 thuyduongbka

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

this solution works with node v20.8.0

arunkmoury avatar Dec 16 '23 13:12 arunkmoury