Repository navigation
fix(build): add .js extensions to relative imports in the ES module build - #2933
Open
joshuablac wants to merge 1 commit into
Open
joshuablac wants to merge 1 commit into
joshuablac wants to merge 1 commit into
Conversation
…uild Node.js requires relative import paths in ES modules to include the file extension, so importing any file from `es/` (for example `validator/es/lib/isEmail.js`) failed with ERR_MODULE_NOT_FOUND on `./util/assertString`. Add a small local Babel plugin, used only by the `es` env in `.babelrc`, that appends `.js` to relative import and re-export paths when the target is a `.js` file, and fails the build if a relative path does not resolve to a file. The CommonJS (`lib/`, `index.js`) and browser builds are unchanged. Fixes validatorjs#2924 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2933 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 114 114
Lines 2599 2599
Branches 658 658
=========================================
Hits 2599 2599 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #2924 (related: #2528)
Problem
Node.js requires relative import paths in ES modules to include the file extension. The files in
es/import each other without one (import assertString from './util/assertString'), so importing anything fromes/in Node fails.Reproduced on Node.js 26.7.0 with the built package copied into an app's
node_modules/validator. Paths below are shortened to/app.Before:
After:
Cause
build:esruns Babel withmodules: false, which keeps theimportstatements as they are written insrc/, without extensions. Bundlers resolve those paths, but Node's ESM resolver does not.Fix
babel-plugin-es-import-extensions.js: a small local Babel plugin (no new dependency). Forimport,export ... fromandexport * fromwith a relative path, it:.jswhen<path>.jsexists;.babelrc: the plugin is added to theesenv only. Thedevelopmentenv (CommonJS build and tests) is unchanged. The browser build usesbabelrc: falseand is unaffected.The plugin file sits at the repo root next to
build-browser.mjs.npm pack --dry-runconfirms that the plugin,.babelrcandtest/are not in the tarball, whilees/still is.I did not change the README example (
import isEmail from 'validator/es/lib/isEmail';). It works with bundlers, and the README's Node ESM import paths are the subject of open PR #2642.Tests
test/esBuild.test.js: walks the builtes/directory and asserts that every relative import path ends in.jsand points to an existing file.index.js imports "./lib/toDate", which is not a path to a file.fs.readdirSync/fs.statSync/fs.existsSync(nowithFileTypes), so it should also run on the Node 8 job in the CI matrix. I have not run it on Node 8 locally.npm test(build + lint + mocha) on Node.js 26.7.0:Other checks on Node.js 26.7.0, comparing the build before and after this change:
lib/,index.js,validator.jsandvalidator.min.jsare byte-identical.es/, 103 of 114 files change, and only on their relative import lines. Each path gains.js, and Babel prints the rewritten paths with double quotes instead of single quotes. The other 11 files have no relative imports and are byte-identical.node_modules/validatorlayout, all 114es/files can be loaded withimport(). Before the change, 103 of them failed withERR_MODULE_NOT_FOUND.es/index.jswith the repo's rollup gives byte-identical output before and after, so bundler users see no change.import validator from 'validator'(CommonJS) andvalidator/lib/isUUID.jsstill work after the change.index.jsandlib/are byte-identical to before.Note: validator's
package.jsonhas no"type": "module". Node therefore loadses/*.jsas ES modules only through module syntax detection, which is on by default in Node.js 26.7.0, the only version I tested. On Node versions that treat these files as CommonJS, importinges/from Node still won't work. This change doesn't alter that.Checklist
This fix was written with AI assistance (Claude Code) and verified locally as described above.
🤖 Generated with Claude Code