Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

[nextjs] Importing OrbitControls resulting to "SyntaxError: Cannot use import statement outside a module" #2059

Closed
5ervant opened this issue May 2, 2020 · 4 comments

Comments

@5ervant
Copy link

5ervant commented May 2, 2020

Description

I'm implementing OrbitControls with react-three-fiber: https://codesandbox.io/s/41zwr

I will just use react-native-webview to support the Expo native version of my Next.js app with Expo for web using @expo/next-adapter.

Behavior

However, it's raising this error when I import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'; :

SyntaxError: Cannot use import statement outside a module

Environment

  Expo CLI 3.13.1 environment info:
    System:
      OS: Windows 10
    Binaries:
      Yarn: 1.16.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
      npm: 6.13.7 - C:\Program Files\nodejs\npm.CMD
      Watchman: 4.9.4 - C:\Users\USER\AppData\Local\watchman\watchman.EXE
    IDEs:
      Android Studio: Version  3.6.0.0 AI-192.7142.36.36.6241897

- Phone/emulator/simulator platform, model and version:
    Chrome-based web browser (updated)

Reference

@5ervant 5ervant changed the title [nextjs] Importing OrbitControls resulting to SyntaxError: Cannot use import statement outside a module [nextjs] Importing OrbitControls resulting to "SyntaxError: Cannot use import statement outside a module" May 4, 2020
@5ervant 5ervant changed the title [nextjs] Importing OrbitControls resulting to "SyntaxError: Cannot use import statement outside a module" [nextjs] Importing OrbitControls resulting to SyntaxError: Cannot use import statement outside a module May 4, 2020
@5ervant 5ervant changed the title [nextjs] Importing OrbitControls resulting to SyntaxError: Cannot use import statement outside a module [nextjs] Importing OrbitControls resulting to "SyntaxError: Cannot use import statement outside a module" May 4, 2020
@5ervant
Copy link
Author

5ervant commented May 4, 2020

This is try to implement and pray.

"setupRegTHREE.js":

import * as THREE from 'three';

global.THREE = global.THREE || THREE;

Component's code:

// ...
import { extend, /* ... */ } from 'react-three-fiber';

import './setupRegTHREE';
import 'three/examples/js/controls/OrbitControls';

extend({ OrbitControls: THREE.OrbitControls });

// And the rest...

@tsogoo
Copy link

tsogoo commented Jan 7, 2021

This is try to implement and pray.

"setupRegTHREE.js":

import * as THREE from 'three';

global.THREE = global.THREE || THREE;

Component's code:

// ...
import { extend, /* ... */ } from 'react-three-fiber';

import './setupRegTHREE';
import 'three/examples/js/controls/OrbitControls';

extend({ OrbitControls: THREE.OrbitControls });

// And the rest...

for me it works.

@danielx-art
Copy link

Didn't work for me, the error is "OrbitControls is not exported from 'three' "

@edtervit
Copy link

edtervit commented Mar 27, 2023

I think it's a problem with Next.Js's SSR, I've tried:

  • Using Next.JS 13.1 Transpile feature and adding drei to the config
  • The above solution from 5ervant

This is what fixed it for me:

in index.js

import dynamic from "next/dynamic";
// component containing the orbit controls is LandingSection
const LandingSection = dynamic(() => import("../components/LandingSection"), { ssr: false });

export default function Home() {
  return (
    <div>
      <LandingSection  />
    </div>
  );
}

The Key is the SSR: false in the dynamic import

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants