validator.js icon indicating copy to clipboard operation
validator.js copied to clipboard

Documentation Issue: Incorrect ES Import Path

Open saar-twito opened this issue 1 year ago • 7 comments

The documentation on npm indicates that tree-shakeable ES imports should be done using: import isEmail from 'validator/es/lib/isEmail.js'. However, the es folder does not exist in the package. Only the path validator/lib/isEmail.js works.

Either the es folder should exist to support ES imports as documented, or the documentation should be updated to reflect the correct import path (validator/lib/isEmail.js).

saar-twito avatar Feb 09 '25 06:02 saar-twito

If I'm looking through the published files in NPM, I do see that file. It's not in the repo since it's generated upon preparing a release. What is the error you are getting?

WikiRik avatar Feb 10 '25 19:02 WikiRik

Noting the error messages received when using ES import syntax from the documentation.

To reproduce: Node 22.x, NPM 10.x, validator: 13.15.0, package.json.type: module.

//import isEmail from 'validator/lib/isEmail'; // Documentation example 1. Throws error snippet below. Cannot find module '{...}/validator/node_modules/validator/lib/isEmail' imported from {...}/validator/app.js Did you mean to import "validator/lib/isEmail.js"?`

//import isEmail from 'validator/es/lib/isEmail'; // Documentation example 2. Throws error snippet below. Cannot find module '{...}/validator/node_modules/validator/es/lib/isEmail' imported from {...}/validator/app.js Did you mean to import "validator/es/lib/isEmail.js"?

import isEmail from 'validator/lib/isEmail.js'; // Refactor with js extension. Works.

// import isEmail from 'validator/es/lib/isEmail.js'; // Refactor 2 with js extension. Throws error snippet below. Cannot find module '{...}/validator/node_modules/validator/es/lib/util/assertString' imported from {...}/validator/node_modules/validator/es/lib/isEmail.js

console.log(isEmail('[email protected]'));`

gordonscott08 avatar May 05 '25 01:05 gordonscott08

I can confirm the same behavior and will add that I get some sort of error no matter how I import the library unless I use the ES6 default import style (import v from 'validator').

critesjm avatar May 27 '25 13:05 critesjm

node esm required to have extensions. that why

import  'isEmail' from 'validator/es/lib/isEmail.js'

try to works. But internal imports should be done with extensions too and hat not happens on file inself.

So build system should be adjust before using esm on nodejs

Systerr avatar Jun 11 '25 21:06 Systerr

Same here, cannot make it work with the syntax import isUUID from 'validator/es/lib/isUUID.js' provided in the README. Only default import works import validator from 'validator'

greenbea avatar Jul 09 '25 16:07 greenbea

Yeah, the problem is that the package internally doesn't use file extensions, and Node ESM imports require it.

I get these errors, (unless I use some sort of bundler to add the file extensions)

Cannot find module /Users/drew/Sites/project/node_modules/validator/es/lib/util/assertString' imported from /Users/drew/Sites/project/node_modules/validator/es/lib/isUUID.js",

It is because the package is missing file extensions of assertString.js for example.

drewbaker avatar Nov 07 '25 22:11 drewbaker

Is this documentation issue or further error, because it seems using:

import from 'validator/es/lib/isAscii' causes errors with vitest when some of dependencies are importing validator this way.

FAIL   frontend/src/app/components/domain-list.component.spec.ts [ src/app/components/domain-list.component.spec.ts ]
Error: Cannot find module \frontend\node_modules\validator\es\lib\isAscii' imported from frontend\node_modules\arnes-ui\fesm2022\arnes-ui.mjs
Did you mean to import "validator/es/lib/isAscii.js"?

And then when importing "validator/es/lib/isAscii.js" it doesn't work, another error:

Error: Cannot find module 'frontend\node_modules\validator\es\lib\util\assertString' imported fromfrontend\node_modules\validator\es\lib\isAscii.js

It works only with "validator/lib/isAscii.js"

kle-pra avatar Aug 24 '26 09:08 kle-pra