standard-things / esm

Tomorrow's ECMAScript modules today!
Other
5.27k stars 147 forks source link

More of a question than Issue about using esm #738

Closed janusqa closed 5 years ago

janusqa commented 5 years ago

I understand i have to put the below in file that is past to node when starting...

require = require('esm')(module /*, options*/);
module.exports = require('./main.index').default;

What about other files in the node app. Do i need to only place this once in the file node calls to start and it will trickle down every where to every file or do i need to put it in every file that needs to use import/export

dnalborczyk commented 5 years ago

Hey @janusqa yes, you would do it only once at the entry point of the project. there you can decide if you want to load it from the cli or as a bridge, not both together.

// index.js, entry point of project, es6 module
import uuid from 'uuid'

// .... more code

from the cli, probably preferred, and quicker, if it fits your environment:

node r -esm index.js

or as node.js script:

// main.js
require = require('esm')(module);
module.exports = require('./index.js');
node main.js

from there on esm resolves everything, no matter if commonjs or, if available, even es6-modules including everything in node_modules (cjs+es6) - also depending on the esm options you want to use.

janusqa commented 5 years ago

@dnalborczyk Thank you for that. If I have a module with bridge esm and someone else downloads this via npm for example but they are also using esm, there would be no conflicts right?

dnalborczyk commented 5 years ago

If I have a module with bridge esm and someone else downloads this via npm for example but they are also using esm, there would be no conflicts right?

that's correct. in that case, as a module author, you'd point main in package.json to your bridge file (main.js) - that would be for users just using node.js with the regular require/commonjs. then ideally you'd also create a module field in package.json pointing to your es6 entry file, which would be picked up by users using esm, or any other module which can make sense of es6 modules (rollup, webpack, etc.)