-
Notifications
You must be signed in to change notification settings - Fork 1
resolve __dirname when is not available #114
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,11 @@ | ||||||||||||||||||||||||||||||||||||
| import path from 'path'; | ||||||||||||||||||||||||||||||||||||
| import {resolve} from 'path'; | ||||||||||||||||||||||||||||||||||||
| // __dirname is not available in ES modules, so we define it here | ||||||||||||||||||||||||||||||||||||
| const __dirname = __dirname || import.meta.dirname; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
| import {resolve} from 'path'; | |
| // __dirname is not available in ES modules, so we define it here | |
| const __dirname = __dirname || import.meta.dirname; | |
| import {resolve, dirname} from 'path'; | |
| import {fileURLToPath} from 'url'; | |
| // __dirname is not available in ES modules, so we define it here | |
| const __dirname = typeof __dirname !== 'undefined' | |
| ? __dirname | |
| : dirname(fileURLToPath(import.meta.url)); |
Copilot
AI
Feb 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of import.meta.dirname requires Node.js 20.11.0 or later, but package.json specifies "engines": { "node": ">=12" }. This creates a version compatibility issue.
In Node.js versions 12-20.10.x, import.meta.dirname is undefined, which would cause the fallback logic to fail (even if the fallback logic itself weren't broken). Consider either:
- Using
import.meta.urlwithfileURLToPathanddirnamefor broader compatibility - Updating the minimum Node.js version requirement in package.json to 20.11.0 or later
Note that import.meta.dirname was only added in Node.js 20.11.0 (released January 2024) and 21.2.0.
| import {resolve} from 'path'; | |
| // __dirname is not available in ES modules, so we define it here | |
| const __dirname = __dirname || import.meta.dirname; | |
| import {resolve, dirname} from 'path'; | |
| import {fileURLToPath} from 'url'; | |
| // __dirname is not available in ES modules, so we define it here | |
| const __filename = fileURLToPath(import.meta.url); | |
| const __dirname = dirname(__filename); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment states that
__dirnameis not available in ES modules, but the code attempts to reference it before defining it. In CommonJS (the current module system used by this package according to package.json),__dirnameIS available as a global. In true ES modules,__dirnamewould cause a ReferenceError.This comment is misleading because:
__dirnameis already availableimport.meta.dirnamemay not be available in older Node.js versions (it was added in Node.js 20.11.0)The comment should clarify the actual use case or be updated to reflect the correct fallback strategy.