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

Question about Node.js module export #71

Closed
vimcaw opened this issue May 27, 2020 · 3 comments
Closed

Question about Node.js module export #71

vimcaw opened this issue May 27, 2020 · 3 comments

Comments

@vimcaw
Copy link
Contributor

vimcaw commented May 27, 2020

I notice that react-babylonjs export as ES Module now, but it only defines a “module” field in package.json, that’s nonstandard, in Node.js ES Module Documents, should define a exports field and shipping .mjs files or set type field to module in package.json.

Partial content of Node.js ES Module Documents

Node.js will treat the following as ES modules when passed to node as the initial input, or when referenced by import statements within ES module code:

  • Files ending in .mjs.
  • Files ending in .js when the nearest parent package.json file contains a top-level field "type" with a value of "module".
  • Strings passed in as an argument to --eval, or piped to node via STDIN, with the flag --input-type=module.

Package Entry Points

In a package’s package.json file, two fields can define entry points for a package: "main" and "exports". The "main" field is supported in all versions of Node.js, but its capabilities are limited: it only defines the main entry point of the package.

The "exports" field provides an alternative to "main" where the package main entry point can be defined while also encapsulating the package, preventing any other entry points besides those defined in "exports". This encapsulation allows module authors to define a public interface for their package.

If both "exports" and "main" are defined, the "exports" field takes precedence over "main". "exports" are not specific to ES modules or CommonJS; "main" will be overridden by "exports" if it exists. As such "main" cannot be used as a fallback for CommonJS but it can be used as a fallback for legacy versions of Node.js that do not support the "exports" field.

@vimcaw
Copy link
Contributor Author

vimcaw commented May 27, 2020

And it also causes ESLint can't resolve module: import-js/eslint-plugin-import#1778

@brianzinn
Copy link
Owner

Thanks for this info - I will set type to module. I removed the UMD export when I redid packaging and fixed dependencies from 2.0.6 to 2.1.0 (the package went from 22.8MB to 1.8MB - and a lot of the 1.8 is typings).

Thanks also for the reference documentation. I will be doing a release on Saturday that is a big cleanup and will make sure this is addressed.

@vimcaw
Copy link
Contributor Author

vimcaw commented Jul 15, 2020

The nonstandard package entry point should be fixed by #76.

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

No branches or pull requests

2 participants