I try to convert a .glb file to .usdz on server side (without browser) like this:
convert.js
const THREE = require('three')
const GLTFLoader = require('three/examples/jsm/loaders/GLTFLoader.js')
const USDZExporter = require('three/examples/jsm/exporters/USDZExporter.js')
const scene = new THREE.Scene()
const gltfLoader = new GLTFLoader()
const usdzExporter = new USDZExporter()
scene.add(new THREE.HemisphereLight())
// load GLTF & export USDZ
const filePath = './model.glb'
gltfLoader.load(filePath, gltf => {
scene.add(gltf.scene)
usdzExporter.parse(scene, result => {
const blob = new Blob([result], { type: 'model/vnd.usdz+zip' })
// const url = URL.createObjectURL(blob)
}, error => {
console.error(error)
})
})
I run the code above with command
node convert.js
and I got error:
Error [ERR_REQUIRE_ESM]: require() of ES Module xxx not supported. Instead change the require of GLTFLoader.js in xxx to a dynamic import() which is available in all CommonJS modules.
Then I use import()
instead, like:
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import { USDZExporter } from 'three/examples/jsm/exporters/USDZExporter.js'
and got the new error:
SyntaxError: Cannot use import statement outside a module
So, my question is:
Is it possible load & export model by three.js
in Node.js without a browser?
Thanks a lot.