-
Notifications
You must be signed in to change notification settings - Fork 945
fix(resolve-extends): add import attribute for JSON config files #4551
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
fix(resolve-extends): add import attribute for JSON config files #4551
Conversation
This pull request is automatically built and testable in CodeSandbox.
To see build info of the built libraries, click here or the icon next to each commit SHA.
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 changes seem reasonable while I'd prefer using require()
for json
files personally instead of checking Node versions.
@JounQin I have replaced previous solution with require()
.
82936c9
into
conventional-changelog:master
Description
Fixes
ERR_IMPORT_ATTRIBUTE_MISSING
error when extending JSON configuration files in Node.js ESM environments. This issue prevents users from using JSON-based commitlint configurations that extend other configs.I had this problem recently, also found an existing issue #3970 reporting the same problem.
Modified the
dynamicImport
function in@commitlint/resolve-extends
to detect JSON files and add the appropriate import attribute syntax based on the Node.js version:{ assert: { type: "json" } }
(import assertion syntax){ with: { type: "json" } }
(import attribute syntax)This ensures backward compatibility across all supported Node.js versions while using the correct syntax for each version. The distinction is important because:
assert
) were the initial implementation available in Node.js 16.15+with
) became the standard and replaced assertions in Node.js 22+assert
syntax entirelySource: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
Motivation and Context
When trying to extend a JSON configuration file, Node.js throws
ERR_IMPORT_ATTRIBUTE_MISSING
because JSON imports in ESM require import attributes to specify the module type. This affects commitlint v19+ when working with JSON config files.Closes #3970
How Has This Been Tested?
Before Fix (Error Case)
npm install
echo "feat: add something" | npx commitlint
ERR_IMPORT_ATTRIBUTE_MISSING
error ❌After Fix (Working Case)
Clone the fix from: https://github.com/macieklamberski/commitlint
Build the packages:
npm install && npm run build
Link the updated packages:
In the cloned example project (https://github.com/macieklamberski/commitlint-json):
Run commitlint with JSON config - no error occurs ✅
Types of changes
Checklist: