# Using GLTFLoader on node

**URL:** <https://discourse.threejs.org/t/using-gltfloader-on-node/2309>\
**Category:** Questions\
**Tags:** node-js, gltf-loader\
**Created:** [April 2, 2018, 9:25pm UTC](https://discourse.threejs.org/t/using-gltfloader-on-node/2309 "2018-04-02T21:25:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ikeough](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/ikeough/32/1444_2.png) [@ikeough](https://discourse.threejs.org/u/ikeough)\
**Post date:** [April 2, 2018, 9:25pm UTC](https://discourse.threejs.org/t/using-gltfloader-on-node/2309/1 "2018-04-02T21:25:33Z")

</div>

I would like to use the GLTFLoader to do some server side loading of models for the purpose of making thumbnail images. Towards this end, I have created a small node script which downloads zip files containing gltf models from S3, unzips them to a `tmp` directory, then attempts to load the models locally.

My first attempt was to use `loader.parse(myGLTF,...)`. This correctly reads the gltf manifest as json, but as soon as it attempts loads the first .bin file, I get an exception `TypeError: Cannot read property 'slice' of undefined.` This exception occurs in `GLTFParser.prototype.loadBufferView`. It seems that it can’t load the local binary files.

For my second attempt, having followed several other threads that suggest to server the files statically, I stood up a server which serves the gltf files (.gltf (JSON) and, .bin (binary)) at [http://localhost:8080/](http://localhost:8080/)… When calling `loader.load("http://127.0.0.1:8080/model0.gltf,...")`, I can see that the XMLHttpRequest is being made, but I get an error, `Unexpected end of JSON input`. It can’t even read the gltf json.

The gltf models I am using are sound. I have successfully loaded the same in the client. And I can use the Threejs gltf preview in VS Code to see the models.

Has anyone successfully used GLTFLoader on the server?

Also, I have attempted to do both of the above with and without a LoadingManager, attempting to use the LoadingManager’s capability to modify urls to use `file://`. Using the LoadingManager callbacks, I can see that my files are being “succesfully loaded”, but the errors remain.

---

<div class="post-metadata">

**Author:** ![donmccurdy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/donmccurdy/32/33209_2.png) [@donmccurdy](https://discourse.threejs.org/u/donmccurdy)\
**Post date:** [April 3, 2018, 12:28am UTC](https://discourse.threejs.org/t/using-gltfloader-on-node/2309/2 "2018-04-03T00:28:47Z")

</div>

I haven’t seen this done yet… if you have some control over the input models, using `.glb` may be simpler as there are no external resources. You should be able to pre-preload the buffer in that case, and call

```auto
loader.parse( buffer, './', (gltf) => { ... })

```

Also see [https://github.com/mrdoob/three.js/pull/13541](https://github.com/mrdoob/three.js/pull/13541)

---

<div class="post-metadata">

**Author:** ![ikeough](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/ikeough/32/1444_2.png) [@ikeough](https://discourse.threejs.org/u/ikeough)\
**Post date:** [April 5, 2018, 5:26am UTC](https://discourse.threejs.org/t/using-gltfloader-on-node/2309/3 "2018-04-05T05:26:21Z")

</div>

I merged the .gltf and .bin files into one .glb and it works as you suggested. Thanks!

---

<div class="post-metadata">

**Author:** ![sayduck-daniel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/sayduck-daniel/32/3772_2.png) [@sayduck-daniel](https://discourse.threejs.org/u/sayduck-daniel)\
**Post date:** [November 10, 2018, 6:20pm UTC](https://discourse.threejs.org/t/using-gltfloader-on-node/2309/4 "2018-11-10T18:20:54Z")

</div>

Im trying to do the same thing here and I encoutered the same problems: load() of a local or distant file fails on `Cannot read property 'slice' of undefined` while trying to read .bin

Now I’m trying to use a glb file but I end up with other errors and I suppose this comes from how I load the glb file:

`loader.parse(Buffer.from(fs.readFileSync(path, 'binary')), ...`

Any idea how to read it properly?

---

<div class="post-metadata">

**Author:** ![muhmundr](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@muhmundr](https://discourse.threejs.org/u/muhmundr)\
**Post date:** [July 7, 2020, 10:46am UTC](https://discourse.threejs.org/t/using-gltfloader-on-node/2309/5 "2020-07-07T10:46:34Z")

</div>

Well… it says “SyntaxError: Unexpected token g in JSON at position 0”

```
const loadGLTFModel = (fileName) => {
  const loader = new THREE.GLTFLoader();
  const data = fs.readFileSync(fileName);
  const buffer = new Buffer.from( data ).toString();
  loader.parse(buffer, './', (gltf) => console.log(gltf));
}

```

It works with “gltf” files, but only to the point of loading .bin files. And then it fails with message “THREE.GLTFLoader: Failed to load buffer” because it requires XMLHttpRequest to load it.

---

<div class="post-metadata">

**Author:** ![donmccurdy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/donmccurdy/32/33209_2.png) [@donmccurdy](https://discourse.threejs.org/u/donmccurdy)\
**Post date:** [July 7, 2020, 5:38pm UTC](https://discourse.threejs.org/t/using-gltfloader-on-node/2309/6 "2020-07-07T17:38:41Z")

</div>

This is a complex topic — it takes a lot of ugly hacks to get code requiring browser APIs to work in node.js. Please review [[Solved] GLTFLoader DRACOLoader in nodejs](https://discourse.threejs.org/t/solved-gltfloader-dracoloader-in-nodejs/5980/12) for some of the steps you’ll need to take, although (depending on what features you need) this may not be completely solved.

---

<div class="post-metadata">

**Author:** ![muhmundr](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@muhmundr](https://discourse.threejs.org/u/muhmundr)\
**Post date:** [July 8, 2020, 8:53am UTC](https://discourse.threejs.org/t/using-gltfloader-on-node/2309/7 "2020-07-08T08:53:52Z")

</div>

Yes, the topic is complex but seems that this is pretty popular topic. I found solution for work with GLB on node.js without textures. It works pretty well. I need only process of the 3d objects without rendering.  
I think that the only thing that should be solved is the Texture loading. I guess that if redefine the load textures function to the node.js function which will read data and convert it to base64 - it will work like a charm.

```
const toArrayBuffer = (buf) => {
  const arrayBuffer = new ArrayBuffer(buf.length);
  const view = new Uint8Array(arrayBuffer);
  for (let i = 0; i < buf.length; ++i) {
    view[i] = buf[i];
  }
  return arrayBuffer;
}

const loadGLTFModel = (fileName) => {
  const loader = new THREE.GLTFLoader();
  return new Promise((resolve, reject) => {
    if (fs.existsSync(fileName)) {
      const data = fs.readFileSync(fileName);
      const arrayBuffer = toArrayBuffer(data);
      loader.parse(arrayBuffer, '',
        (object3D) => {
          resolve(object3D);
        },
        (error) => {
          console.log(error);
          reject('Loader failed')
        });
    } else reject(`Cannot find ${fileName}`);
  });
}
```

---

<div class="post-metadata">

**Author:** ![muhmundr](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@muhmundr](https://discourse.threejs.org/u/muhmundr)\
**Post date:** [July 8, 2020, 6:24pm UTC](https://discourse.threejs.org/t/using-gltfloader-on-node/2309/8 "2020-07-08T18:24:17Z")

</div>

I think this is a kind of “ugly hack” that you had in mind, right? 🙂

> [@Using GLB with textures on node](https://discourse.threejs.org/t/using-glb-with-textures-on-node/16854/2):
>
> For those who are looking for the answer to the question about load GLB models on node.js with three.js Here’s a working solution for models WITHOUT textures: const toArrayBuffer = (buf) =\> { const arrayBuffer = new ArrayBuffer(buf.length); const view = new Uint8Array(arrayBuffer); for (let i = 0; i \< buf.length; ++i) { view[i] = buf[i]; } return arrayBuffer; } const loadGLTFModel = (fileName) =\> { const loader = new THREE.GLTFLoader(); return new Promise((resolve, reject) =\>…

---

<div class="post-metadata">

**Author:** ![okonno](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/okonno/32/3195_2.png) [@okonno](https://discourse.threejs.org/u/okonno)\
**Post date:** [February 26, 2021, 9:21pm UTC](https://discourse.threejs.org/t/using-gltfloader-on-node/2309/9 "2021-02-26T21:21:49Z")

</div>

this did work great, thanks.  
I was just wanting to compute the bounding boxes server-side for simple physics, and all I had to do was make  
`GLTFParser.prototype.loadTextureImage`  
and  
`GLTFLightsExtension.prototype.createNodeAttachment`  
return early null, because they reference DOM `self` (window) var

---

<div class="post-metadata">

**Author:** ![DREAMER505](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/dreamer505/32/26216_2.png) [@DREAMER505](https://discourse.threejs.org/u/DREAMER505)\
**Post date:** [November 27, 2022, 3:30am UTC](https://discourse.threejs.org/t/using-gltfloader-on-node/2309/10 "2022-11-27T03:30:27Z")

</div>

there are many issues in the process with gltfLoader,whitch one of is Error: THREE.GLTFLoader: No DRACOLoader instance provided.
