validator.js
validator.js copied to clipboard
SyntaxError: Cannot use import statement outside a module
Hi, I clone and fork the project, and to test if everything is working fine or not I am running the below code with help of the tem.js file.
import all from'./src/index.js';
const {isUrl}=all
console.log(isUrl('zeel'));
But, I am getting the below Error
js/tem.js"
(node:1491723) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/zeel/Desktop/open_sources/validator.js/tem.js:1
import all from'./src/index.js';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:1001:16)
at Module._compile (internal/modules/cjs/loader.js:1049:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47
I also set "type": "module" in the package.json. but that requires the .js at the end of every import file. is there is another way to solve this error?
Thank you.
Hi @zeel-codder, did you try to require the index.js
file at the root of the repo ?
const validator = require('./index.js')
console.log(validator.isEmail('[email protected]'))
@0kyn in that way he would use commonjs modules and I am not sure if it's what he's trying to achieve.
@zeel-codder to use import ... from ...
you have to enable ES modules either in your package.json
or in your tsconfig
(if you are using typescript).
However ES modules are "quite a new" feature and they are not yet supported everywhere, so you may want to use that syntax but to have the legacy approach of commonjs modules. To accomplish the latter you should use a transpiler (e.g. Babel or tsc) to convert your ES6 code to older formats.
To use import ... from ...
with NodeJS I had to use the following argument:
node --es-module-specifier-resolution=node ./file.js
To use
import ... from ...
with NodeJS I had to use the following argument:node --es-module-specifier-resolution=node ./file.js
I try but then I am getting this Error.
import toDate from './lib/toDate';
^^^^^^
SyntaxError: Cannot use import statement outside a module
@0kyn in that way he would use commonjs modules and I am not sure if it's what he's trying to achieve.
@zeel-codder to use
import ... from ...
you have to enable ES modules either in yourpackage.json
or in yourtsconfig
(if you are using typescript). However ES modules are "quite a new" feature and they are not yet supported everywhere, so you may want to use that syntax but to have the legacy approach of commonjs modules. To accomplish the latter you should use a transpiler (e.g. Babel or tsc) to convert your ES6 code to older formats.
I added this is package.json file.
"type": "module"
But then I am getting below Error.
internal/process/esm_loader.js:74
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/zeel/Desktop/open_sources/validator.js/src/lib/toDate' imported from /home/zeel/Desktop/open_sources/validator.js/src/index.js
at finalizeResolution (internal/modules/esm/resolve.js:271:11)
at moduleResolve (internal/modules/esm/resolve.js:694:10)
at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:805:11)
at Loader.resolve (internal/modules/esm/loader.js:88:40)
at Loader.getModuleJob (internal/modules/esm/loader.js:241:28)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:72:40)
at link (internal/modules/esm/module_job.js:71:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
Also, I want to Ask When we Crate Node App in that app we can use any type of import it works fine but in this project is not working why.?
If u are using js file with modules in HTML file then use :
<script type="module" src="index.js"></script>
and If U are using Js file with modules and work with console or any framework or etc. then U should create a package.json file in your folder with "type":"module" using npm init -y command
I think there is not a clear answer here yet. I'm trying out the library in an toy express application with mongoose. Here is what I have so far...
// package.json
{
"name": "myapp",
"version": "0.0.1",
"main": "index.js",
"description": "My awesome app",
"type": "module",
"scripts": {
"start": "node index.js",
"start:dev": "nodemon index.js"
},
"dependencies": {
"express": "^4.18.1",
"http-status-codes": "^2.2.0",
"mongoose": "^6.6.5",
"validator": "^13.7.0"
},
"volta": {
"node": "16.17.1",
"npm": "8.19.2"
},
"devDependencies": {
"dotenv": "^16.0.3",
"nodemon": "^2.0.20"
}
}
// root/models/users.model.js
import mongoose from 'mongoose';
import validator from 'validator'; // this works
import isEmail from 'validator/es/lib/isEmail/isEmail.js' // doesn't work
import isEmail from 'validator/lib/isEmail' // this works but then I need an import for each validator
import isDate from 'validator/lib/isDate' // this works but then I need an import for each validator
const userSchema = new mongoose.Schema({
email: {
type: String,
trim: true,
require: 'Email is required',
unique: true,
validate: [validator.isEmail, 'Email is invalid'] // this works
},
...
})
const UserModel = mongoose.model('Users', userSchema)
export { UserModel }
Is there a way to use the syntax import { isEmail, isDate } from 'validator'
. I guess that one doesn't want to import the entire library if he/she is just going to use a handful of validators.
Getting the same error in "all-ESM" monorepo. I think it comes down to the import from validator/es/lib/blah.js
(ofc, can't forget the file extension in ESM) looking up the package.json
of the validator
package in node_modules and complaining that it doesn't have "type": "module"
.
Getting the same error. Here is what worked for me:
import IsEmail from 'validator/lib/isEmail.js';
IsEmail.default(value);
I was receving the same in a node environment (mono repo using all ESM packages) where i was importing from the tree-shakeable path:
import isEmail from 'validator/es/lib/isEmail';
Importing from the following paths cleared the error for me:
import isEmail from 'validator/lib/isEmail';