question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SyntaxError: Cannot use import statement outside a module

See original GitHub issue

When importing zod as an esm module, a syntax error is thrown:

import z from 'zod'
^^^^^^

SyntaxError: Cannot use import statement outside a module

Reproduction steps

mkdir test
cd test
echo '{"type":"module"}' > package.json
yarn add zod@3.0.0-alpha.9
echo "import z from 'zod'" > index.js
node index.js
$ node --version
v15.11.0
$ yarn --version
1.22.10

OS: Pop!_OS 20.10 x86_64 (basically Ubuntu 20.10)

Possible fixes

I haven’t pulled anything down to fix it just yet, but the thing it’s complaining about is that in order for native esm modules to import other modules, you need to have "type": "module" in your package.json. I don’t know off the top of my head what that will do to commonjs. When I tried just editing it directly in node_modules, the next issue that came up is that the imports inside of lib/esm/ didn’t have extentions, i.e. import * as z from "./external" instead of import * as z from "./external.js".

I’ll try pulling it down and document what I’ve tried here.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
colinhackscommented, Mar 23, 2021

This should be at least partially fixed with the most recent release. I switched to using Rollup to bundle the ES module which sidesteps the file extension issue.

You should always do import { z } from 'zod'.

I read the Node docs thoroughly and set up Zod’s package.json to support conditional exports. I thought this would enable you to import Zod without setting "type": "module" in the package.json (of the consuming project, not in the Zod package) but it didn’t work for me (with Node 14, --experimental-modules). I guess I’m misunderstanding something.

Anyway this should be largely resolved.

@ksmithut @beejunk Give it a try with alpha.30 or later and let me know if it works.

1reaction
ksmithutcommented, Mar 27, 2021

Fixed for me as well 😃 Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Uncaught SyntaxError: Cannot use import statement outside ...
This means that you're using the native source code in an unaltered/unbundled state, leading to the following error: Uncaught SyntaxError: ...
Read more >
How to fix "cannot use import statement outside a module"
I stumbled on this error: Uncaught SyntaxError: cannot use import statement outside a module while importing a function from a JavaScript file.
Read more >
Cannot use import statement outside a module [React ...
When building a web application, you may encounter the SyntaxError: Cannot use import statement outside a module error.
Read more >
How to solve: cannot use import statement outside a module
When you see the error message Uncaught SyntaxError: cannot use import statement outside a module, it means you're using an import statement ......
Read more >
Cannot use import statement outside a module - Future Studio
A common error with modules is the “Uncaught SyntaxError: Cannot use import statement outside a module”. This error means you must ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found