title: SyntaxError: Cannot use import statement outside a module (from dependency)
body: <p><strong>How do you resolve &quot;Cannot use import statement outside a module&quot; from a dependency when the dependency isn't declared as a module?</strong></p>
  <hr />
  <p>I want to use the <a href="https://www.npmjs.com/package/validator" rel="noreferrer">validator</a> in Svelte/kit to validate emails. However, when importing the ESM version, I get the &quot;Cannot use import statement outside a module&quot; error. I'm using pnpm instead of npm or yarn.</p>
  <pre><code>import isEmail from 'validator/es/lib/isEmail'
  </code></pre>
  <pre><code>/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 
  </code></pre>
  <p>It appears that validator is attempting to use the import statement, but it's package.json does not specify <code>&quot;type&quot;: &quot;module&quot;</code>. My guess is that this is the root cause of the error.</p>
  <h3>Debug steps</h3>
  <ul>
  <li>package.json has <code>&quot;type&quot;: &quot;module&quot;</code></li>
  <li>Upgraded to latest version of node</li>
  <li>Tried using the non-esm version of validator <code>'validator/lib/isEmail'</code>, but that causes other errors not related to this thread.</li>
  </ul>
  <h3>Related</h3>
  <ul>
  <li><a href="https://stackoverflow.com/questions/58384179/syntaxerror-cannot-use-import-statement-outside-a-module">SyntaxError: Cannot use import statement outside a module</a> — this solution is for code you control, not for dependencies</li>
  <li><a href="https://stackoverflow.com/questions/58211880/uncaught-syntaxerror-cannot-use-import-statement-outside-a-module-when-import">&quot;Uncaught SyntaxError: Cannot use import statement outside a module&quot; when importing ECMAScript 6</a> — this seems to be very similar, but I'm getting this error in node, not in the browser</li>
  </ul>
  <h3>Metadata</h3>
  <ul>
  <li>Node: v16.2.0</li>
  <li>Sveltekit: v1.0.0-next.115</li>
  <li>Validator: 13.6.0</li>
  </ul>