How to import OBJ using OBJLoader2

I’m trying to load an obj but when I run it, I get an error in the console that says “Uncaught SyntaxError: Cannot use import statement outside a module”

Here’s my code:

import { OBJLoader2 } from 'three/examples/jsm/loaders/OBJLoader2';

// instantiate the loader
let loader = new OBJLoader2();

// function called on successful load
function callbackOnLoad ( object3d ) {
    scene.add( object3d );
}

// load a resource from provided URL synchronously
loader.load( 'God.obj', callbackOnLoad, null, null, null );

As the error message says you are trying to use ES6 import syntax outside of a module context. This usually happens when you import modules within ordinary <script> tags. It only works if you add the attribute type="module" similar to the official three.js examples.

I had the same issue . So I removed the

import { OBJLoader2 } from 'three/examples/jsm/loaders/OBJLoader2';

And added the

<script src="https://rawcdn.githack.com/mrdoob/three.js/r96/examples/js/loaders/OBJLoader.2js"></script>

above tag in the head section to link to the OBJLoader2 file and it worked fine for me… :slight_smile: