Skip to content
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

What is commonjs2 ? #1114

Closed
Sinewyk opened this issue May 25, 2015 · 8 comments
Closed

What is commonjs2 ? #1114

Sinewyk opened this issue May 25, 2015 · 8 comments
Labels

Comments

@Sinewyk
Copy link

@Sinewyk Sinewyk commented May 25, 2015

Sorry if that's obvious but it's been eating me for multiple days and even greping commonjs2 through this repository I couldn't find any obvious information.
My google-fu on commonjs2 led me back to #864 ... which was not helpful.

Anyone could explicitely inform me what is commonjs2, the differences with commonjs, and what is the relation with webpack ?

Thanks ...

@sokra
Copy link
Member

@sokra sokra commented May 26, 2015

CommonJs spec defines only exports. But module.exports is used by node.js and many other CommonJs implementations.

commonjs mean pure CommonJs
commonjs2 also includes the module.exports stuff.

@Sinewyk
Copy link
Author

@Sinewyk Sinewyk commented May 26, 2015

So ... the replacement of module.exports

module.exports = function(){};

instead of simply adding stuff to the exports object

exports.a = 'a';
exports.b = 'b;'

is not in the "initial" commonjs spec ? nodejs started it and everybody followed ?

@bebraw bebraw added the question label Nov 14, 2015
@bebraw
Copy link
Member

@bebraw bebraw commented Nov 14, 2015

@Sinewyk Yeah, pretty much. See https://nodejs.org/docs/latest/api/modules.html for reference.

@vamsiampolu
Copy link

@vamsiampolu vamsiampolu commented Jul 31, 2017

Are there other commonjs implementations that do not use module.exports which need to be supported by webpack? Now that I understand what the two options commonjs and commonjs2 are, I would like to understand if there is a practical use case for commonjs.

stevemao added a commit to stevemao/serverless-webpack that referenced this issue Sep 26, 2018
stevemao added a commit to stevemao/serverless-webpack that referenced this issue Sep 29, 2018
@njleonzhang
Copy link

@njleonzhang njleonzhang commented Feb 11, 2019

As described in the document, commonjs belongs to Expose Via Object Assignment, commonjs2 belongs to Module Definition Systems.

Although the output of this two build(exports['MyLibrary'] = _entry_return_; and module.exports = _entry_return_;) is so similar, it seems Webpack treat them differently in importing. For example,

I have a simple module:

// const.js
export let name = 'leon'
export default function print() { console.log('print: ' + name) }

I build this module as commonjs and commonjs2.

Then, in another project, I try to import both modules, then I found Webpack can only handle commonjs2 correctly.

import Const1, { name as name1 } from '../output/dist/index.cmd.js'
import Const4, { name as name4 } from '../output/dist/index.cmd2.js'

console.log(Const1, name1)
/*
  Const1 is {
    MyLibrary: {
        default: print(){...},
        name: 'leon'
    }
  }

  name1 is undefined
*/

console.log(Const4, name4)  // Const4 is print function, name4 is 'leon'

So it seems we should build the module as commonjs2 instead of commonjs, if we want to exports the moudle. This is why commonjs2 belongs to Module Definition Systems? 😂

@slavafomin
Copy link

@slavafomin slavafomin commented Mar 15, 2019

I'm also curious. As @vamsiampolu asked, is there a real use case for commonjs export variant? Is it actually publicly used in practice?

@xgqfrms-GitHub
Copy link

@xgqfrms-GitHub xgqfrms-GitHub commented Dec 6, 2019

react & react-dom

also using commonjs2 module

image

image

@kwasimensah
Copy link

@kwasimensah kwasimensah commented Jul 9, 2020

Note, if you you use an external that starts with "@" and you're webpacking for the server you need to use commonjs2 per https://stackoverflow.com/questions/58105364/add-dependency-to-externals-with-in-the-library-name

To make this googleable:
from Terser Unexpected character '@'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
8 participants
You can’t perform that action at this time.