Subwaytime / vite-aliases

Alias auto generation for Vite
https://www.npmjs.com/package/vite-aliases
MIT License
213 stars 13 forks source link

Error: Dynamic require of "path" is not supported #33

Closed ivarlovlie closed 2 years ago

ivarlovlie commented 2 years ago

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

Subwaytime commented 2 years ago

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

Subwaytime commented 2 years ago

@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!

ivarlovlie commented 2 years ago

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

Subwaytime commented 2 years ago

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 commented 2 years ago

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.

ivarlovlie commented 2 years ago

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 commented 2 years ago

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

Subwaytime commented 2 years ago

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 commented 2 years ago

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.

jcoble commented 2 years ago

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 commented 2 years ago

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.

Subwaytime commented 2 years ago

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

jcoble commented 2 years ago

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.

Subwaytime commented 2 years ago

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

Tommertom commented 2 years ago

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

Subwaytime commented 2 years ago

fix is coming with the newest release!

Subwaytime commented 2 years ago

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!

Aurorxa commented 2 years ago

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

image

Subwaytime commented 2 years ago

@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

MattMarquise commented 2 years ago

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

binulad commented 1 year ago

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()],
})
MoySantanaV commented 1 year ago

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

izetter commented 1 year ago

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

thuyduongbka commented 1 year ago

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

arunkmoury commented 11 months ago

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

this solution works with node v20.8.0