-
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
resolve __dirname when is not available #114
Conversation
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.
Pull request overview
This pull request attempts to improve ES module compatibility in src/SqliteExtensions.js by updating the path module import and adding support for __dirname in ES modules. However, the implementation contains critical bugs that will cause runtime errors.
Changes:
- Changed from default
pathimport to namedresolveimport - Added a
__dirnamedefinition to handle ES module environments - Updated all path resolution calls to use
resolvedirectly
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/SqliteExtensions.js
Outdated
| import {resolve} from 'path'; | ||
| // __dirname is not available in ES modules, so we define it here | ||
| const __dirname = __dirname || import.meta.dirname; |
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.
This line has a logical error: const __dirname = __dirname || import.meta.dirname; references __dirname on the right side before it's defined. This will result in a ReferenceError when __dirname is not available (i.e., in ES modules).
For ES module compatibility, the correct approach is to use import.meta.url with the fileURLToPath and dirname functions from the 'path' and 'url' modules:
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __dirname = import.meta.url ? dirname(fileURLToPath(import.meta.url)) : __dirname;Alternatively, if using Node.js 20.11.0 or later where import.meta.dirname is available, use a try-catch or conditional check that doesn't reference the undefined variable.
| 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)); |
src/SqliteExtensions.js
Outdated
| @@ -1,9 +1,11 @@ | |||
| import path from 'path'; | |||
| import {resolve} from 'path'; | |||
| // __dirname is not available in ES modules, so we define it here | |||
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 comment states that __dirname is 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), __dirname IS available as a global. In true ES modules, __dirname would cause a ReferenceError.
This comment is misleading because:
- The source files use ES6 import/export syntax but are transpiled to CommonJS by Babel/Rollup
- When running in CommonJS (the default format),
__dirnameis already available - When running as an ES module,
import.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.
src/SqliteExtensions.js
Outdated
| import {resolve} from 'path'; | ||
| // __dirname is not available in ES modules, so we define it here | ||
| const __dirname = __dirname || import.meta.dirname; |
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); |
This pull request updates the build configuration to improve compatibility with ES modules by replacing usage of
__dirnameand introducing a new plugin dependency. The most important changes are grouped below:Build configuration improvements:
rollup.config.jsto use therollup-plugin-replaceplugin, replacing all occurrences of__dirnamewithimport.meta?.dirnameto better support ES module environments. [1] [2]rollup-plugin-replaceas a development dependency inpackage.json.