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.

Cannot use import statement outside a module

See original GitHub issue

Describe 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

  1. Create a default NextJS project by running npx create-next-app and go to that folder
  2. Install the OL package by running npm i -P ol
  3. Open the file pages/index.js
  4. Add the line import Map from 'ol/Map' at the top of the file
  5. Run the project with npx next
  6. 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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

19reactions
AndrewRayCodecommented, Mar 29, 2020

I’m running into this issue with react-spring:

/node/app/node_modules/react-spring/three.js:1
import * as THREE from 'three';
^^^^^^

From this line in my app:

import { useSpring } from 'react-spring/three';

I get this error when I npm run build.

The above import statement is in a component I load purely dynamically. The troublesome import is in Content.tsx, which I load in pages/index.tsx with:

import React from 'react';
import dynamic from 'next/dynamic';

const ContentDynamic = dynamic(() => import('./Content'), {
  ssr: false
});

const HomePage = () => {
  return <ContentDynamic />;
};

export default HomePage;

Is Nextjs unable to detect Content.tsx is only a client side component?

react-spring does appear to publish a cjs version, so this removes the error:

import { useSpring } from 'react-spring/three.cjs';

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?

6reactions
benojcommented, Dec 2, 2020

@Timer when you suggest:

1.Instruct webpack to not exclude the package for the server bundle (Next.js does not bundle server-dependencies by default, which your custom setup might have).

How exactly does one do this? I can’t find any examples around…

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 >
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 >

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