Skip to main content
Notice removed Draw attention by Nick
Bounty Ended with Leftium's answer chosen by Nick
added 216 characters in body
Source Link
Nick
  • 6.6k
  • 4
  • 40
  • 78

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/[email protected]/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

Metadata

  • Node: v16.2.0
  • Sveltekit: v1.0.0-next.115
  • Validator: 13.6.0

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/[email protected]/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

Metadata

  • Node: v16.2.0
  • Sveltekit: v1.0.0-next.115
  • Validator: 13.6.0

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/[email protected]/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

Metadata

  • Node: v16.2.0
  • Sveltekit: v1.0.0-next.115
  • Validator: 13.6.0
Notice added Draw attention by Nick
Bounty Started worth 100 reputation by Nick
Source Link
Nick
  • 6.6k
  • 4
  • 40
  • 78

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/[email protected]/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

Metadata

  • Node: v16.2.0
  • Sveltekit: v1.0.0-next.115
  • Validator: 13.6.0