-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
esm: add gotcha to documentation #32237
Conversation
Update es module documentation in accordance to nodejs#31710 by providing a gotcha for windows users
Explains that absolute specifiers and absolute paths are not the same, and mentions what to do about it. Refs: nodejs#31710
Thanks for the input! Now it's stuck on a js linting. Like this:
Should I just remove the "js" tag and be done with it? |
Node.js not support |
|
||
const fileUrl = pathToFileURL('c:\\path\\to\\file.js').href; | ||
|
||
await import(fileUrl); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
example:
await import(fileUrl); | |
(async function() { | |
await import(fileUrl); | |
})(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel the code snipped should be short and to the point. An iife is a bit distracting. Maybe adding a ".then(...)" instead of await?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the docs PR @lulzmachine it's a huge help to clarify these things better in the docs.
|
||
const fileUrl = pathToFileURL('c:\\path\\to\\file.js').href; | ||
|
||
await import(fileUrl); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a comment above noting what the value of fileUrl
is:
// file:///c:/path/to/file.js
or similar, just so it is clear what is happening.
@@ -801,6 +801,22 @@ property: | |||
|
|||
## Differences Between ES Modules and CommonJS | |||
|
|||
### Absolute file paths don't work on Windows |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have a section on module specifiers in the "Terminology" section above. I'd rather we move this note into that section along with the description of module specifiers, than as a specific difference between CJS and ESM.
Strictly speaking, yes it is a difference, but I think to try keep the docs organized we should keep sections together.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense. But this note does need to specifically mention Windows, for Ctrl+F compatibility. My idea was that platform differences belonged in a "gotchas" type section.
But I'm new to writing the node.js docs. Would you rather have it up by the "specifiers" section?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I can get behind having this section be a specific compat issue section.
Can we change the title then to:
### Absolute file paths don't work on Windows | |
### File URLs not file paths |
and move it to after the extensions section, since that should probably still come first.
Then perhaps with the description, we can get straight to the problem rather than having it in the title:
On Windows, importing file paths will throw since the drive letter is interpreted
as a URL protocol. Instead a URL conversion is needed...
or something like that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you're importing a path, conversion is always needed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm concerned that's a bit backwards; I would prefer to catch Windows' users eyes with the title, to make sure they understand it's an important section. And then go into the technical reasons about why it doesnt work for them in the body.
But again, I'm new here. Is your method more inline with how the docs are typically worded?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. My primary concern was to bring the documentation in line with experience for Windows users (and for makers of libraries supporting Windows).
Another way to resolve this issue seems to be to change the implementation of ES Modules in node.js to enforce the specifiers not allowing a starting char of '/'. And to change this documentation PR to one that is general for both MacOS and Windows ("File URLs not file paths").
Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is valid for a relative url to start with /
and i don't see any reason to limit that. In terms of changes to the runtime, adding a more detailed error message in ERR_UNSUPPORTED_ESM_URL_SCHEME when the scheme is a single letter might help.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree for the windows-specific bug, adding more context to the error message would be preferable. Then to keep the docs changes here in line with the suggestion above to provide the more general advice since this is not Windows-specific information (eg there are other character encoding issues that apply for all platforms).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be a really good way to highlight the error when it happens. Definitely a good change!
Regarding absolute file paths when running on MacOS. Can we put some code to detect if the user is trying to do absolute path imports, and warn them that this is unsupported? Like comparing the fileToUrl() output with "file://" + specifier to see if it's the same?
Right now there's the issue of library creators using import()
in unsupported ways. For libraries that auto-import files, it's likely that they will implement loading on MacOS, find that absolute paths work (by luck) and ship that. Which causes errors for Windows users of those libraries.
This already happened with jest
and babel
. But I'm sure more lower-profile projects will follow, resulting in undetected non-platform-independent libraries on npm.
I think that there is an expectation that you can produce platform-independent code by sticking with the built in functions (except for the obvious ones like child_process
and some fs
functions). And I would like to explore what possibilities there are to keep it that way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The last subsection of the “Differences between ES Modules and CommonJS” section is “URL-based paths”. I think that the warning should go in there, either inside that section or in a new section after it.
All the other gotchas are platform-agnostic, and therefore more important than something Windows-specific (since they affect everyone, not just Windows users). I understand that Windows users are a majority of all users, but absolute paths are also not that common of a use case; when combining both a gotcha that doesn’t affect everyone with a feature that isn’t that commonly used (absolute paths), I think it should go near the end of the list.
```js | ||
import { pathToFileURL } from 'url'; | ||
|
||
const fileUrl = pathToFileURL('c:\\path\\to\\file.js').href; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const fileUrl = pathToFileURL('c:\\path\\to\\file.js').href; | |
const fileUrl = pathToFileURL('C:\\path\\to\\file.js').href; |
uppercase is necessary. We are using C:
everywhere else in those docs.
related PR: #32294
@@ -801,6 +801,22 @@ property: | |||
|
|||
## Differences Between ES Modules and CommonJS | |||
|
|||
### Absolute file paths don't work on Windows |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The last subsection of the “Differences between ES Modules and CommonJS” section is “URL-based paths”. I think that the warning should go in there, either inside that section or in a new section after it.
All the other gotchas are platform-agnostic, and therefore more important than something Windows-specific (since they affect everyone, not just Windows users). I understand that Windows users are a majority of all users, but absolute paths are also not that common of a use case; when combining both a gotcha that doesn’t affect everyone with a feature that isn’t that commonly used (absolute paths), I think it should go near the end of the list.
on linux and MacOS, and therefore they are also accepted. | ||
But on Windows, importing an _absolute path_ to a file will not work. | ||
To ensure cross platform compatibility, file paths should be converted into | ||
URLs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a matter of style, the Node docs avoid italics/unnecessary emphasis whenever possible.
8ae28ff
to
2935f72
Compare
This issue/PR was marked as stalled, it will be automatically closed in 30 days. If it should remain open, please leave a comment explaining why it should remain open. |
Closing this because it has stalled. Feel free to reopen if this PR is still relevant, or to ping the collaborator who labelled it stalled if you have any questions. |
Update es module documentation in accordance to #31710 by providing a gotcha for windows users
Checklist