mmomtchev/SharedMap

Confusing naming convention

Closed this issue · 6 comments

I use this with node and I noticed that this new commit doesn't really work.

I looked around, found out that somehow the main entry in package.json points to a UMD file, which isn't the correct format required by Node.js.

And then I looked into index.js and it was a ES module. I looked at index.es.js and somehow it is both CommonJS and ES? (there are module.exports but below it was a export statement. What?) I am very confused and had no idea what was happening.

What is your environment and what error message do you get?
index.js is not an ES module, it is a pure vanilla JS with no external dependencies
index.es.js is an ES module, you are right that it should not contain module.exports

I removed the module.exports from index.js and added linting for the generated files, check to see if it solves your problem

> require("./index.js")
C:\Users\Takase\Documents\stuffs\helpme\pino-discord-webhook2\node_modules\sharedmap\index.js:88
export class SharedMap {
^^^^^^

Uncaught SyntaxError: Unexpected token 'export'

This is what I get from index.js. This is clearly an ES module since it has the export statement.

But the index.es.js is also an ES module because it has export too.

Not to mention the package.json uses a UMD module as main, which is incompatible with CommonJS that Node.js uses.

I think what you meant to do is have:

  • index.js that is CommonJS
  • index.es.js that is ES module
  • index.umd.js which is a UMD module

What you should do is:

  • Write your code as an ES module
  • Compile it to CommonJS module and UMD module
  • Add the CommonJS module file to main in package.json

You could also utilize rollup + babel to compile it down to older ES versions, but its optional.

You are not supposed to use index.js directly
You should require index.umd.js if you are using CommonJS or UMD
Or import index.es.js if you are using ES modules
The package.json has both main (index.umd.js) and module (index.es.js), so normally all this should be completely automatic depending on your environment

Yeah tested it just now, it does work. I probably need to check my code sometimes later after I went to school today. Sorry for the trouble.

Yes, it works in my projects too. Thank you for your help. Closing this issue.