Esm: More of a question than Issue about using esm

Created on 7 Mar 2019  路  3Comments  路  Source: standard-things/esm

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

question

Most helpful comment

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.)

All 3 comments

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.

@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?

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.)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mAAdhaTTah picture mAAdhaTTah  路  3Comments

ericelliott picture ericelliott  路  3Comments

deepsweet picture deepsweet  路  3Comments

kherock picture kherock  路  3Comments

OmgImAlexis picture OmgImAlexis  路  3Comments