Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples: Add package.json indicating jsm examples are modules #21838

Merged
merged 1 commit into from
May 17, 2021

Conversation

gkjohnson
Copy link
Collaborator

Related issue: #19575 (comment)

Description

As @donmccurdy suggested this adds a package.json file in examples/jsm to indicate that the contained files are es6 modules so they can be loaded correctly in node. In current master node will try (and fail) to load the examples/jsm files as commonjs because "type: module" is not present in the root package.json. Tested by making a test node project with npm and adding the file to the node_modules/three/examples/jsm folder.

The true end of an era? Hopefully 🤞

@mrdoob mrdoob added this to the r129 milestone May 17, 2021
@mrdoob mrdoob merged commit a943d00 into mrdoob:dev May 17, 2021
@mrdoob
Copy link
Owner

mrdoob commented May 17, 2021

Thanks!

@gkjohnson gkjohnson deleted the examples-module-package branch May 17, 2021 15:40
@donmccurdy
Copy link
Collaborator

donmccurdy commented May 21, 2021

I think we should go ahead with the change, but will note that it appears to send users of ts-node into a known bug of that project, I get an ERR_REQUIRE_ESM error when doing something like this:

ts-node script.ts
// script.ts
import { BoxBufferGeometry, BufferGeometry, Group, Matrix4, Mesh, Vector3 } from 'three';
import { Geometry } from 'three/examples/jsm/deprecated/Geometry';
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: $PROJECTS/three.js/examples/jsm/deprecated/Geometry.js
require() of ES modules is not supported.
require() of $PROJECTS/three.js/examples/jsm/deprecated/Geometry.js from $PROJECTS/three-to-cannon/test/index.test.ts is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename Geometry.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from $PROJECTS/three.js/examples/jsm/package.json.

Strictly speaking I think that's ts-node's problem (TypeStrong/ts-node#155 (comment)), and we're doing the right thing here. Node.js ecosystem has some catching up to do with ES Modules, although it's headed that way. Similar issue in another project, here: sindresorhus/path-type#9.

Prior to the change I was using esm to make this work; it doesn't seem to have any effect here. The remaining workaround for Node.js users would be compile their code to JS first and then run it, rather than having ts-node do this in one step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants