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 (in Vite development server) #219

Closed
Nye1nChanSoe opened this issue May 10, 2023 · 2 comments
Closed

__dirname is not defined (in Vite development server) #219

Nye1nChanSoe opened this issue May 10, 2023 · 2 comments

Comments

@Nye1nChanSoe
Copy link

Vite Plugin Version

0.7.5

Laravel Version

10.10.0

Node Version

18.6.0

NPM Version

9.5.1

Operating System

Windows (WSL)

OS Version

Windows 10 Pro: 22H2

Web browser and version

Google Chrome Version 113.0.5672.92 (Official Build) (64-bit)

Running in Sail?

No

Description

I created a new Laravel application by using Laravel installer v:4.5.0

The development server shows an error message that __dirname is not defined
However, even the error message is displayed, the development server continues to run and serves the application.
could be related to changes in the way Node.js handles the __dirname variable in ES modules.

Apart from this error message everything works fine.

My current approach might not be the most elegant or optimal solution, I would like to offer my temporary workaround for handling the error.

import { dirname } from "path";
import { fileURLToPath } from "URL";

And I have added the expression dirname(fileURLToPath(import.meta.url) in the place of previous __dirname

It would look like this
fs.readFileSync(path.join(dirname(fileURLToPath(import.meta.url)), "dev-server-index.html")).toString().replace(/{{ APP_URL }}/g, appUrl)

return JSON.parse(fs.readFileSync(path.join(dirname(fileURLToPath(import.meta.url)), "../package.json")).toString())?.version;

Steps To Reproduce

laravel new app

npm install

npm run dev

@timacdonald
Copy link
Member

See #216

@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

No branches or pull requests

2 participants