Skip to content

Instantly share code, notes, and snippets.

@guybedford
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guybedford/a1155602a0ff41b22359 to your computer and use it in GitHub Desktop.
Save guybedford/a1155602a0ff41b22359 to your computer and use it in GitHub Desktop.
Interpreting CommonJS modules in ES6 environments

Question - should import {fs} from 'fs' be supported for CommonJS module loading from ES6?

If it is to be supported, we would effectively define the fs module object in the ES6 registry to look something like the following:

var fs = require('fs');
var _fs = {};
Object.keys(fs).forEach((p) => _fs[p] = fs[p]);
_fs.default = fs;

new Module(_fs) // this is taken to be the module in the registry

This way I can import from the CommonJS module with default and named export syntax:

import fs from 'fs';
import {readFile} from 'fs';

We can choose to support this or not. But it would be great to get agreement.

@sheerun
Copy link

sheerun commented Nov 23, 2014

I don't understand the (p) => _fs[p] = fs[p] part. Why not just:

var fs = require('fs');
fs.default = fs;
new Module(fs)

@briandipalma
Copy link

@sheerun it's to allow named exports too. Otherwise you would only have the default export.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment