Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__dirname is not defined #67

Closed
tinytini12 opened this issue Sep 5, 2024 · 20 comments · Fixed by #70
Closed

__dirname is not defined #67

tinytini12 opened this issue Sep 5, 2024 · 20 comments · Fixed by #70

Comments

@tinytini12
Copy link

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
Copy link
Owner

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

@tinytini12
Copy link
Author

tinytini12 commented Sep 9, 2024

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
Copy link
Owner

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
Copy link
Owner

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

@cmurtaugh
Copy link

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

@protibimbok
Copy link
Owner

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

@cmurtaugh
Copy link

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

@tinytini12
Copy link
Author

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

@protibimbok
Copy link
Owner

I have published the changes. Please check.

@tinytini12
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

tinytini12 commented Oct 7, 2024

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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

@tinytini12
Copy link
Author

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 protibimbok linked a pull request Oct 18, 2024 that will close this issue
@protibimbok
Copy link
Owner

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

@tinytini12
Copy link
Author

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

Thank you for all the work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants