fix(resolve-extends): add import attribute for JSON config files #4551
+6
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: