__dirname in dependency not being handled by Vite #8482
Replies: 3 comments 2 replies
-
|
I am having the same problem with jimp.js. The offending line is const dir = process.env.DIRNAME || I tried to set DIRNAME in my root .env file but I keep getting the same error message:
Is there a way to set or activate __dirname so that a module which I don't control can consume it? |
Beta Was this translation helpful? Give feedback.
-
|
Please check the type in your import { resolve, dirname } from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename) |
Beta Was this translation helpful? Give feedback.
-
|
In my case I'm trying to use Vite alongside Electron and whenever I import something like BrowserWindow from it, I'll get this exact error. My type is not |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to build a React component library. Buried in my dependency tree is a component (xml-parse) that uses __dirname in an attempt to do some dynamic requires. When I build my component, everything seems to run fine (that is no errors). However, when I load my component in my demo app in the browser, I run into "__dirname is not defined". When I look at the build file, sure enough the __dirname reference is still there.
Build vite.config.js
Dependency code that persists through the build (from xml-parse):
Please note: the __dirname references in the vite.config.js file are working fine. This is a dependency several layers down my dependency tree that is using __dirname and it isn't getting resolved. I also do not have the ability to effect changes to the dependencies which use xml-parse.
I'm really at a loss for what to do here and nearly every search brings up something that just isn't helpful. I'm sure there is something I'm overlooking since I'm very new to this. The documentation seems to suggest that all references to __dirname are handled but clearly isn't. I know of several other teams that are using the library successfully on the browser (but they don't use Vite).
Beta Was this translation helpful? Give feedback.
All reactions