protibimbok / django-vite-plugin

This plugin configures Vite for use with Django backend.
112 stars 14 forks source link

__dirname is not defined #67

Closed tinytini12 closed 1 month ago

tinytini12 commented 2 months ago

Hi! With last release and using as ESM, it's showing this error:

Screenshot 2024-09-05 142730

How to fix: https://flaviocopes.com/fix-dirname-not-defined-es-module-scope/

protibimbok commented 2 months ago

Can you please give me a way to reproduce this? I know it should have happened but can't seem to encounter it.

tinytini12 commented 2 months ago

You can use this repo: Repo updating django-vite-plugin package to its latest version, running npm run dev and opening http://localhost:5173/

protibimbok commented 2 months ago

I don't know why but I can not re create this. Please check the examples of this repo & create a pr with your problem.

protibimbok commented 2 months ago

You may check #69 and see if it fixes your problem.

cmurtaugh commented 1 month ago

I'm also having this problem; how can I install this branch in order to test?

protibimbok commented 1 month ago

Using pnpm First clone this repo and cd into vite directory. Then run:

pnpm run build

You can then install it in two ways:

  1. Export a tarball and install from there

    pnpm pack

    And then cd into your project and install it:

    pnpm install ../path/to/tarball
  2. Install from directory: cd into your project and run

    pnpm install ../path/to/cloned_repo/vite
cmurtaugh commented 1 month ago

Thanks! I was able to get the PR branch installed and it did seem to fix the problem!

tinytini12 commented 1 month ago

Couldn't try this solution. This command throws and error in my computer pnpm run build

protibimbok commented 1 month ago

I have published the changes. Please check.

tinytini12 commented 1 month ago

Fails for me: [vite] Internal server error: ENOENT: no such file or directory, open 'C:\C:\Users\my-user\my-folder\projects\my-project\node_modules\django-vite-plugin\dist\esm\info.html'

protibimbok commented 1 month ago

Couldn't try this solution. This command throws and error in my computer pnpm run build

Please try the same branch again. And let me know the error you are facing (if any)

tinytini12 commented 1 month ago

Fails for me: [vite] Internal server error: ENOENT: no such file or directory, open 'C:\C:\Users\my-user\my-folder\projects\my-project\node_modules\django-vite-plugin\dist\esm\info.html'

Its giving me two errors, the first one is Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'glob' and once I install that, the second is the one above.

protibimbok commented 1 month ago

First pull #69 again And then:

Using pnpm First clone this repo and cd into vite directory. Then run:

pnpm run build

You can then install it in two ways:

  1. Export a tarball and install from there
pnpm pack

And then cd into your project and install it:

pnpm install ../path/to/tarball
  1. Install from directory: cd into your project and run
pnpm install ../path/to/cloned_repo/vite

Did you follow these steps and still got no file error?

tinytini12 commented 1 month ago

First pull #69 again And then:

Using pnpm First clone this repo and cd into vite directory. Then run:

pnpm run build

You can then install it in two ways:

  1. Export a tarball and install from there
pnpm pack

And then cd into your project and install it:

pnpm install ../path/to/tarball
  1. Install from directory: cd into your project and run
pnpm install ../path/to/cloned_repo/vite

Did you follow these steps and still got no file error?

Yes, that's what I did

protibimbok commented 1 month ago

With type = module set in the package.json file please run the following code in terminal and send me the result.

function getAbsolutePathFromMetaUrl(path: string): string {
    if (process.platform === 'win32' && path.startsWith('/')) {
        return path.substring(1)
    }
    return path
}
console.log(getAbsolutePathFromMetaUrl(
        typeof __dirname === 'undefined'
            ? // @ts-ignore
              path.dirname(new URL(import.meta.url).pathname)
            : __dirname,
))
tinytini12 commented 1 month ago

Result

With type = module set in the package.json file please run the following code in terminal and send me the result.

function getAbsolutePathFromMetaUrl(path: string): string {
    if (process.platform === 'win32' && path.startsWith('/')) {
        return path.substring(1)
    }
    return path
}
console.log(getAbsolutePathFromMetaUrl(
        typeof __dirname === 'undefined'
            ? // @ts-ignore
              path.dirname(new URL(import.meta.url).pathname)
            : __dirname,
))

Result: C:\Users\my-user\my-folder\projects\my-project

protibimbok commented 1 month ago

Result: C:\Users\my-user\my-folder\projects\my-project

In that case, you are not supposed to get the error:

Fails for me: [vite] Internal server error: ENOENT: no such file or directory, open 'C:\C:\Users\my-user\my-folder\projects\my-project\node_modules\django-vite-plugin\dist\esm\info.html'

Check: https://github.com/protibimbok/django-vite-plugin/pull/70/commits/a98c5c7ee2894a576e58900f81b3c7b6f171e2a6

It uses the exact same code to resolve the path.

Please make sure you are in the correct branch. Reference: #70

tinytini12 commented 1 month ago

Result: C:\Users\my-user\my-folder\projects\my-project

In that case, you are not supposed to get the error:

Fails for me: [vite] Internal server error: ENOENT: no such file or directory, open 'C:\C:\Users\my-user\my-folder\projects\my-project\node_modules\django-vite-plugin\dist\esm\info.html'

Check: a98c5c7

It uses the exact same code to resolve the path.

Please make sure you are in the correct branch. Reference: #70

You're right! in branch __dirname_fix it works

protibimbok commented 1 month ago

Thank you for the report. It's been updated, alhamdulillah. Please update to the latest version.

tinytini12 commented 1 month ago

Thank you for the report. It's been updated, alhamdulillah. Please update to the latest version.

Thank you for all the work!