SyntaxError: Cannot use import statement outside a module
See original GitHub issueWhen 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:
- Created 3 years ago
- Reactions:1
- Comments:7 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
Fixed for me as well 😃 Thanks!