-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
Docs: Add sample code ES Modules format to the examples in the README. #61
Comments
Ok from me adding explicit cjs/esm. There is essentially only one line different though, so I think there could be cjs/esm example code but there does not need to be two versions of the example calls. And to make it more obvious the only different is the import, I would put the require/import on the first line so the code is the same for the other two lines. (I feel putting extra code on the same line as the require is "clever" and compact, but clearer to do require on a line on its own.) Wait for comment from @ljharb too (other maintainer). (I assume we can't easily do the cjs/esm tab thing like on the nodes.org web site.) |
I guess we can include both, sure, but knowing how CJS works is something beginners need to learn anyways. No, we can’t do tabs like that. Doing the require on the same line is good to show because that’s a feature ESM lacks. However we do it, I’d only want to see the two additional import lines, and nothing else duplicated. |
Thank you both for your prompt feedback. Based on your suggestions, I'm thinking of proceeding by placing the Feedback-Based Suggestion//for CommonJS
// ./example/parse.js
const argv = require('minimist')(process.argv.slice(2));
//for ES Modules
// ./example/parse.mjs
import minimist from 'minimist';
const argv = minimist(process.argv.slice(2));
//Common
console.log(argv);
|
Lots of comments, I think a lot about READMEs, sorry. 😅 I like the idea of having example files for both, which is implied by your description. Of course! I was forgetting we had an explicit matching example file, small as it is. I think having both imports and the For interest, a couple of patterns I have used elsewhere.
Trying out some of those ideas, see what you think. exampleExample files: example/parse.js (CommonJS) example/parse.mjs (ECMAScript)
|
Thanks for the great ideas! |
This issue will be closed as the related PRs have been successfully merged. |
How about adding ES Modules format sample code to the examples in the README?
If you accept my idea, I will create a PR.
My Suggestion
I confirmed it works with Node.js version 20.9.0
CommonJS
ES Modules
The text was updated successfully, but these errors were encountered: