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 #216

Closed
sp1r1t opened this issue May 9, 2023 · 2 comments · Fixed by #217
Closed

__dirname is not defined #216

sp1r1t opened this issue May 9, 2023 · 2 comments · Fixed by #217

Comments

@sp1r1t
Copy link

sp1r1t commented May 9, 2023

Vite Plugin Version

0.7.5

Laravel Version

10.8

Node Version

18.16.0

NPM Version

9.6.6

Operating System

macOS

OS Version

13.3.1

Web browser and version

Google Chrome Version 112.0.5615.137 (Official Build) (arm64)

Running in Sail?

Sail

Description

After a fresh laravel installation I get this error.

__dirname is not defined
    at file:///var/www/html/node_modules/laravel-vite-plugin/dist/index.mjs:128:39
    at call (file:///var/www/html/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:49019:7)
    at next (file:///var/www/html/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:48963:5)
    at file:///var/www/html/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:61287:17
    at viteHtmlFallbackMiddleware (file:///var/www/html/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:61367:16)
    at call (file:///var/www/html/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:49019:7)
    at next (file:///var/www/html/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:48963:5)
    at viteServeStaticMiddleware (file:///var/www/html/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:46030:20)
    at call (file:///var/www/html/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:49019:7)
    at next (file:///var/www/html/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:48963:5
Click outside or fix the code to dismiss.
You can also disable this overlay by setting server.hmr.overlay to false in vite.config.js.

index.mjs:128 is:

fs.readFileSync(path.join(__dirname, "dev-server-index.html")).toString().replace(/{{ APP_URL }}/g, appUrl));

It seems to me __dirname can not be used because it is an ES module.

Adding these lines in before line 128 fixes the problem for me:

import * as url from 'url';
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

Steps To Reproduce

  1. Install laravel
curl -s "https://laravel.build/example-app" | bash
  1. Start laravel
cd example-app
sail up -d
  1. Start vite
sail yarn run dev
  1. Go to vite in the browser
    http://localhost:5173/
@timacdonald
Copy link
Member

PR up to address this one: #217

Until this is tagged avoid visiting the dev server URL.

@timacdonald
Copy link
Member

Please upgrade to 0.7.6. Sorry for the troubles, folks.

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.

2 participants