Search code examples
javascriptnode.jses6-moduleseventemitter

What's the difference between import from 'events' vs. import {EventEmitter} from 'events'?


Seems both can work. the two e1 listed below are both EventEmitters and can be used that way.

import { EventEmitter } from "events";
let e1 = new EventEmitter();

import events from "events";
let e1 = new events();

I googled and found no answers. I also researched node docs and found the the differences starts from node 4.x (at 0.12.x, it was still import from Events.EventEmitter).

Could anyone help explain what's the difference here?


Solution

  • When you use curly braces { } while importing from a module, You are directly importing specific export from a module called named exports and not the whole module.

    Modules can have a default export which can be imported without using the curly braces. import MyModule from './MyModule . This default export may give you access to other exports of the module depending how the module handles it's exports.