Skip to content

Instantly share code, notes, and snippets.

@brianneisler
Last active November 27, 2022 08:23
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save brianneisler/d008f8b9df344acc4aae47f9db049023 to your computer and use it in GitHub Desktop.
Save brianneisler/d008f8b9df344acc4aae47f9db049023 to your computer and use it in GitHub Desktop.
Cheat sheet - es6 import/export mixed with require/modules.exports
require('./module.js') // { a: 1 }
import module from './module.js' // undefined
import { a } from './module.js' // 1
require('./module2.js') // { default: { a: 1 }, b: 2 }
import module2 from './module2.js' // { a: 1}
import { b } from './module2.js' // 2
require('./module3.js') // { default: { a: 1 }, b: 2 }
import module3 from './module3.js' // { a: 1}
import { b } from './module3.js' // 2
// module.js
module.exports = {
a: 1
}
// module2.js
module.exports = {
'default': {
a: 1
},
b: 2
}
// module3.js
export default { a: 1 }
const b = 2
export b
@calvintychan
Copy link

calvintychan commented Nov 23, 2016

Awesome. Makes total sense.

@Skycocoo
Copy link

Skycocoo commented Jul 24, 2018

require('./module3.js')              // { default: { a: 1 }, b: 2 }
import module3 from './module2.js'   // { a: 1}
import { b } from './module2.js'     // 2

should be:

require('./module3.js')              // { default: { a: 1 }, b: 2 }
import module3 from './module3.js'   // { a: 1}
import { b } from './module3.js'     // 2

(changed module2.js to module3.js)

@brianneisler
Copy link
Author

Thanks @Skycocoo. I've updated the gist.

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