Skip to content
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

"Cannot find module" error for lib or es/lib modules #2387

Open
OneComputerGuy opened this issue Apr 1, 2024 · 1 comment
Open

"Cannot find module" error for lib or es/lib modules #2387

OneComputerGuy opened this issue Apr 1, 2024 · 1 comment
Labels

Comments

@OneComputerGuy
Copy link

OneComputerGuy commented Apr 1, 2024

Issue:

When importing either a subset of libraries or the tree-shakeable library for any validator using ES6 modules, a NodeJS ES6 error is shown indicating that the module cannot be found

Examples

image

This is the package.json file:

{
  "name": "testines6",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "type": "module",
  "dependencies": {
    "validator": "^13.11.0"
  }
}

And the file calling the library (index.js):

import isAlpha from "validator/es/lib/isAlpha";

const check = (input) => isAlpha(input);

check('blablabla');

This has been reported here: #1759 (comment) but using either option (lib or es/lib) shows the same error.

The only path going forwards seems to be importing the entire library and using the function from there which creates a bundle file with unnecessary information

EDIT:

To isolate the issue, ran the same setup on a brand new Google Cloud VM using the same NodeJS and validator version. Ran into the same issue for both es/lib and /lib imports but it works importing the entire library

image

Additional context
Validator.js version: 13.11.0
Node.js version: 20.10.0
OS platform: macOs

@pajasevi
Copy link

The issue is that this library isn't really ESM compliant because package.json does not have the exports field which would export all of these modules and thus standard ES module import won't work without a bundler that would try to fix that.

So until that is fixed, only was to import via ESM is to use the default export.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants