Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 567
user3236895 Avatar asked Dec 06 '25 08:12

user3236895


1 Answers

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.

like image 140
Deepak Yadav Avatar answered Dec 07 '25 21:12

Deepak Yadav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!