Convert .glb format file to .usdz without browser

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.

You’re right to use import { ... } instead of require(), but Node.js needs a “hint” that your script is using ES Modules (import) instead of CommonJS (require). To provide that hint you can do either one of two things:

  • (A) Rename your convert.js to convert.mjs
  • (B) Add a package.json in the project directory containing {"type": "module"}

Yes ~
I rename the file to .mjs. And it works … partially…

A new error:
ReferenceError: ProgressEvent is not defined

in node_modules/three/build/three.module.js:43871

const event = new ProgressEvent( 'progress', { lengthComputable, loaded, total } );

Since ProgressEvent belongs to WebAPI,

I give up this plan.
Trying to find other covert tool.

Thank you all the same.
Have a good day & Happy Halloween ~