Cannot use import statement outside a module
See original GitHub issueDescribe the bug
Importing an OL component using syntax like import Map from 'ol/Map' does not work with NextJS framework.
Note: Not sure if it’s related to NextJS or OpenLayers, but I have no problem importing OL modules in a custom webpack project that is not using NextJS, so my guess is that there is some settings missing by default.
To Reproduce
- Create a default NextJS project by running
npx create-next-appand go to that folder - Install the OL package by running
npm i -P ol - Open the file
pages/index.js - Add the line
import Map from 'ol/Map'at the top of the file - Run the project with
npx next - Go to http://localhost:3000/
Expected behavior The import syntax of OL component should not fail when accessing the page.
System info Next: 9.1.6 Node: 12.14.0 OS: KDE neon User Edition 5.17 x86_64
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:11 (3 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 >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 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 ...
Read more >Cannot use import statement outside module in JavaScript
The "SyntaxError: Cannot use import statement outside a module" occurs when we use the ES6 Modules syntax in a script that was not...
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 >
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

I’m running into this issue with react-spring:
From this line in my app:
I get this error when I
npm run build.The above
importstatement is in a component I load purely dynamically. The troublesome import is inContent.tsx, which I load inpages/index.tsxwith:Is Nextjs unable to detect
Content.tsxis only a client side component?react-springdoes appear to publish acjsversion, so this removes the error:But this no longer has typescript types, and I don’t see why it should have to be a special case import. Is there a way in Nextjs to mark a component as purely dynamic?
@Timer when you suggest:
How exactly does one do this? I can’t find any examples around…