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

doc: confusing support for extensionless ESM #30655

Closed
azz opened this issue Nov 26, 2019 · 5 comments
Closed

doc: confusing support for extensionless ESM #30655

azz opened this issue Nov 26, 2019 · 5 comments
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. experimental Issues and PRs related to experimental features.

Comments

@azz
Copy link
Contributor

azz commented Nov 26, 2019

From the ESM docs (emphasis mine):

import statements of .js and extensionless files are treated as ES modules if the nearest parent package.json contains "type": "module".

Unless I am mistaken (discussed in nodejs/modules#443), extensionless module resolution is not currently supported.

How can you have an import statement for an extensionless file when it does not resolve?

@azz
Copy link
Contributor Author

azz commented Nov 26, 2019

Ah I forgot about --es-module-specifier-resolution=node. Perhaps worth leaving a link to this flag in that area of the docs?

@yorkie
Copy link
Contributor

yorkie commented Nov 26, 2019

It seems that is documented at that page, see: https://nodejs.org/api/esm.html#esm_code_package_json_code_code_type_code_field.

@azz
Copy link
Contributor Author

azz commented Nov 26, 2019

It's documented at the very bottom of the page. Maybe update the example to say something like this? I feel like a lot of people are (like me!) going to run into this when switching to the native ESM loader.

 // my-app.js, part of the same example as above
 import './startup.js'; // Loaded as ES module because of package.json
+
+// Requires --es-module-specifier-resolution=node
+import './startup'; // Loaded as ES module because of package.json

azz added a commit to azz/node that referenced this issue Nov 26, 2019
@MylesBorins
Copy link
Member

Commented in the PR

TL;DR
Extensionless files, as documented, are different from extensionless specifiers.

extensionless filepath and specifier

  • path /path/to/file
  • specifier import './file'

filepath with extension and extensionless specifier

  • path /path/to/file.js
  • specifier import './file'

@devsnek devsnek added esm Issues and PRs related to the ECMAScript Modules implementation. experimental Issues and PRs related to experimental features. labels Dec 3, 2019
@Alhadis
Copy link
Contributor

Alhadis commented Dec 7, 2019

I have an extensionless file (which is an executable) failing to be imported, despite package.json containing "type": "module":

λ JG (master): cat ./test.mjs
#!/usr/bin/env node

import "./bin/list";
λ JG (master): head bin/list
#!/usr/bin/env node
import {join, resolve} from "path";


λ JG (master): ./test.mjs
(node:4311) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/run_main.js:50
    internalBinding('errors').triggerUncaughtException(
                              ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension: /Users/john/Labs/JG/bin/list
    at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:114:13)
    at Loader.resolve (internal/modules/esm/loader.js:74:33)
    at Loader.getModuleJob (internal/modules/esm/loader.js:148:40)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:41:40)
    at link (internal/modules/esm/module_job.js:40:36) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

This happens both with static and dynamic imports.

Current Node version: v13.2.0

BridgeAR pushed a commit that referenced this issue Jan 3, 2020
Fixes: #30655

PR-URL: #30657
Reviewed-By: Yorkie Liu <yorkiefixer@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
MylesBorins pushed a commit that referenced this issue Jan 12, 2020
Fixes: #30655

PR-URL: #30657
Reviewed-By: Yorkie Liu <yorkiefixer@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
BethGriggs pushed a commit that referenced this issue Feb 6, 2020
Fixes: #30655

PR-URL: #30657
Reviewed-By: Yorkie Liu <yorkiefixer@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. experimental Issues and PRs related to experimental features.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants