NiklasMH
NiklasMH

Reputation: 767

Three.js STL loader with TypeScript - Uncaught TypeError: Cannot read property 'XHRLoader' of undefined

I am trying to import an STL file to Three.js, but the three-stl-loader npm-module does not find the THREE object.

This code:

import * as THREE from 'three'
import * as THREESTLLoader from 'three-stl-loader'

var STLLoader = new THREESTLLoader()
var loader = new STLLoader(THREE)
loader.load('path/to/file.stl', function (geometry: any) {})

Produces this error: enter image description here

On line 45 in index.js (in the stl-loader), the three-stl-loader module makes use of the THREE object, but it seems like the module does not know about the THREE object at all: enter image description here

The project is compiled with TypeScript in Webpack. I think there may be a problem with the way it is compiled.

Upvotes: 1

Views: 1841

Answers (1)

NiklasMH
NiklasMH

Reputation: 767

Seems like i found the missing part. As the module, and not the constructor, has to receive the THREE object, I only needed to add THREE to the module instead:

import * as THREE from 'three'
import * as THREESTLLoader from 'three-stl-loader'

var STLLoader = new THREESTLLoader(THREE) // Added THREE
var loader = new STLLoader() // Removed THREE
loader.load('path/to/file.stl', function (geometry: any) {})

Upvotes: 2

Related Questions