Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Docs/review of various affects of type field #475

Open
bmeck opened this issue Jan 17, 2020 · 4 comments
Open

Docs/review of various affects of type field #475

bmeck opened this issue Jan 17, 2020 · 4 comments

Comments

@bmeck
Copy link
Member

bmeck commented Jan 17, 2020

"type" in package.json has a few affects:

  • when loading the main entrypoint of the process or a worker thread it can determine which loader to use: ESM loader (if package.type is "module") vs CJS loader (if type is anything else? [this seems a forwards compat bug?] )
  • manipulates format resolution within its package boundary
    • .js and extension-less files in particular
    • also .node
  • causes the CJS loader to error for CJS format files that end in .js within that package boundary (not extension-less files?)

We should probably document and/or bugfix these if they are not the expected behavior.

@bmeck bmeck added the modules-agenda To be discussed in a meeting label Jan 17, 2020
@jkrems
Copy link
Contributor

jkrems commented Jan 17, 2020

Thanks for collecting that list!

when loading the main entrypoint of the process or a worker thread it can determine which loader to use: ESM loader (if package.type is "module") vs CJS loader (if type is anything else?

That sounds like a bug or at least unexpected behavior. The loader for the entry point should be determined by the file format of the entry point, not by the closest package.json#type. E.g. if I'm in type:module and run node foo.cjs, I wouldn't expect the import loader to be involved.

causes the CJS loader to error for CJS format files that end in .js within that package boundary (not extension-less files?)

Yep, sounds inconsistent to me. It should complain on all "explicitly ESM or otherwise incompatible files", regardless of the type field and which of the well-known ESM types are present. There are some challenges with how loader hooks could influence format determination though.

@bmeck
Copy link
Member Author

bmeck commented Jan 17, 2020

That sounds like a bug or at least unexpected behavior. The loader for the entry point should be determined by the file format of the entry point, not by the closest package.json#type. E.g. if I'm in type:module and run node foo.cjs, I wouldn't expect the import loader to be involved.

It is if a .cjs or .mjs is seen, but the algorithm is actually a bit more complex to reason about than just that, see shouldUseESMLoader's impl

@GeoffreyBooth
Copy link
Member

  • manipulates format resolution within its package boundary

    • .js and extension-less files in particular
    • also .node

For .js and extensionless this was the intended behavior, though I think we should talk about whether extensionless files should be supported. I think that kind of slipped in without much consideration.

.node should be unaffected by "type". Whether import works or doesn’t work for .node files is not something I think we’ve decided; the docs say “An import statement can reference an ES module or a CommonJS module. Other file types such as JSON or Native modules are not supported. For those, use module.createRequire().” So either the docs are wrong or the implementation is wrong, but for sure the "type" field should have no effect on whether import of .node succeeds.

  • causes the CJS loader to error for CJS format files that end in .js within that package boundary (not extension-less files?)

Just as require('./file.mjs') throws, so should require('./file.js') under type: module since those .js files are defined as being ESM/equivalent to .mjs files. Also require('extensionless') should also throw under type: module.

@MylesBorins
Copy link
Contributor

removing the modules-agenda label for now. Please feel free to re-add

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

No branches or pull requests

4 participants