Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

401
Vistas
SyntaxError: Cannot use import statement outside a module (from dependency)

How do you resolve "Cannot use import statement outside a module" from a dependency when the dependency isn't declared as a module?


I want to use the validator in Svelte/kit to validate emails. However, when importing the ESM version, I get the "Cannot use import statement outside a module" error. I'm using pnpm instead of npm or yarn.

import isEmail from 'validator/es/lib/isEmail'
/node_modules/.pnpm/validator@13.6.0/node_modules/validator/es/lib/isEmail.js:1
import assertString from './util/assertString';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:355:18)
    at wrapSafe (node:internal/modules/cjs/loader:1039:15)
    at Module._compile (node:internal/modules/cjs/loader:1073:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
    at Module.load (node:internal/modules/cjs/loader:989:32)
    at Function.Module._load (node:internal/modules/cjs/loader:829:14)
    at Module.require (node:internal/modules/cjs/loader:1013:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at nodeRequire 

It appears that validator is attempting to use the import statement, but it's package.json does not specify "type": "module". My guess is that this is the root cause of the error.

Debug steps

  • package.json has "type": "module"
  • Upgraded to latest version of node
  • Tried using the non-esm version of validator 'validator/lib/isEmail', but that causes other errors not related to this thread.

Related

  • SyntaxError: Cannot use import statement outside a module — this solution is for code you control, not for dependencies
  • "Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6 — this seems to be very similar, but I'm getting this error in node, not in the browser

Metadata

  • Node: v16.2.0
  • Sveltekit: v1.0.0-next.115
  • Validator: 13.6.0
over 4 years ago · Santiago Trujillo
5 Respuestas
Responde la pregunta

0

Try importing like this

import { isEmail } from "validator"
over 4 years ago · Santiago Trujillo Denunciar

0

Without more information, if your problem solely lies in a problem with code you are dependent on, consider short term using something like patch-package to make the necessary adjustment, and long term, open up a PR over at the validator.js repo!

for patch-package, just go add the "type": "module" to the package.json at node_modules/validator, and then run npx patch-package validator. You can then version control the outputted diff file, and the changes are made automatically with npm hooks assuming patch-package is a (dev) dependency.

The behavior or defect of the library here is probably from the devs more keeping in mind things like webpack that do esm imports on their own terms, and not with the node module resolution patterns that it appears svelte is using. or at least how the problem has gotten this far.(might be wrong about this!).

over 4 years ago · Santiago Trujillo Denunciar

0

Try to import like below

const {isEmail} = require('validator');
over 4 years ago · Santiago Trujillo Denunciar

0

Have you tried importing like this?

import validator from 'validator'

I tried reproducing your issue with latest SvelteKit. This works fine:

// index.svelte
<script>
    import validator from 'validator';
    let result = validator.isEmail('foo@bar.com');
    console.log(result);
</script>

When I changed the import statement to:

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

I got the error from your question (Cannot use import statement outside a module).

Importing validator/es/lib/isEmail supposedly only imports a subset of the library. I'm not sure how much difference it will make; it might not make any difference. A slightly larger build beats a build that doesn't work. I suggest getting it working first, then optimize the build size if you really need to.

over 4 years ago · Santiago Trujillo Denunciar

0

After reading their docs: https://github.com/validatorjs/validator.js#es6, it looks like the es part in the import is, so it can be treeshakable. You can fix this error, by just importing it normally:

import isURL from 'validator/lib/isURL';
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda