Add "type" : "module" to package.json

I am trying to use babylon directly from Node without any transform and I’m running into trouble because the ES6 version does not specify "type" : "module" in its package.json file even thought the code is ES6 syntax.
Would it break anything to add "type" : "module" to package.json ?
To reproduce:

$ mkdir babylon
$ cd babylon/
$ echo '{"type":"module"}' >package.json
$ echo 'import "@babylonjs/core/Misc/logger.js"' >test.js
$ npm install @babylonjs/core --save

added 2 packages, and audited 3 packages in 2s

found 0 vulnerabilities
$ node --experimental-vm-modules test.js 
(node:277260) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/didier/tmp/babylon/node_modules/@babylonjs/core/Misc/logger.js:131
export { Logger };
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1026:15)
    at Module._compile (node:internal/modules/cjs/loader:1061:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:168:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:337:24)

Node.js v17.4.0
$ # Adding "type" : "module" to package.json fixes the error.
$ sed -i -e '2 i"type\":\"module\",\n' node_modules/@babylonjs/core/package.json  
$ node --experimental-vm-modules test.js 
$ # No error anymore

we are working on improving this. I hope that 5.0.0 will be released with a revised package.json, including correct peer dependencies and other amended properties.

1 Like

Thanks ! If the principle is accepted, can I send a PR to add this ?

we are not adding it yet, this need to be tested and validated before.

I would usually tell you - please submit the PR, but in this case there are other things involved that need to be tested. so for the time being I would ask you to be patient with this.