DEV Community

Cover image for require Vs import In JavaScript

require Vs import In JavaScript

Travis Ramos on August 15, 2024

I remember when I started coding I would see some js files using require() to import modules and other files using import. This always confused me ...
Collapse
 
mikec711g profile image
Michael Casile

Thanks for this article. I run into lots of nasty messages when I import commonJS libraries into my Angular (client) code. It works fine, but I'm sure I'm importing more than necessary ... and the ability to generate PDFs is too good a feature to give up.

Collapse
 
travislramos profile image
Travis Ramos

You're welcome, glad you enjoyed it!

I also write a weekly newsletter for developers, check it out if you'd like! travislramos.beehiiv.com/subscribe

Collapse
 
baratchela profile image
Michel Baratella

Always good to learn what goes on under the hood when using require vs. import, thanks for the great post!

Collapse
 
travislramos profile image
Travis Ramos

You're welcome, I'm glad you enjoyed it!

Collapse
 
nikita_pawar_80f3fa93c8af profile image
Nikita Pawar

Why do we need to use require for dynamic or run time loading?

Collapse
 
travislramos profile image
Travis Ramos

That's a great question.

So the way import works is that it runs at the start of a script since it is statically analyzed.

When you use require(), you can put it in a condition and only load that module based on a specific condition. Since require() can be invoked at any point in your code, this allows it to be dynamic.

Collapse
 
travislramos profile image
Travis Ramos

Also, if you found this article helpful, I write a weekly newsletter for devs to help them level up and learn along with me! You can check it out here: travislramos.beehiiv.com/subscribe

Collapse
 
sivakumar_siddineni_669b8 profile image
Sivakumar Siddineni

It's good article

Collapse
 
travislramos profile image
Travis Ramos

Thank you, if you'd like to see more content similar to this, give my newsletter a follow! I send it out weekly to over 100 developers like you!

travislramos.beehiiv.com/subscribe

Collapse
 
cos_cos_0a45b58e3827cc418 profile image
Cos Cos

Cool

Collapse
 
heybaldur profile image
HeyBaldur

Great! Thanks for sharing!

Collapse
 
travislramos profile image
Travis Ramos

No problem, glad you enjoyed it!

If you'd like more content similar to this, I write a weekly newsletter that I send out to developers. You can find it here: travislramos.beehiiv.com/subscribe

Collapse
 
aditya26002 profile image
Aditya Daksh

Damn man, i was just facing confusion between these two just moments ago. Thank you very much

Collapse
 
travislramos profile image
Travis Ramos

You're welcome, I'm glad it helped!

I also write a weekly newsletter with similar content and other cool things I think would be helpful for other develoeprs. Check it out if you'd like! travislramos.beehiiv.com/subscribe

Collapse
 
tungtrangn profile image
Trang Tung Nguyen

Thank you for sharing!

Collapse
 
travislramos profile image
Travis Ramos

Yea, no problem! If you found it useful, I also send out a weekly newsletter with content similar to this, plus any useful links I've found throughout the week that I think others would find useful as well. Check it out here: travislramos.beehiiv.com/subscribe

Collapse
 
sugriv_cilans_5cac8ef32f3 profile image
Sugriv cilans

Thank you, it cleared up a lot of doubts about import vs require

Collapse
 
travislramos profile image
Travis Ramos

You're welcome, I'm glad I could help!

If you'd like to receive similar content to your email, I write a weekly newsletter that other developers find useful! Check it out: travislramos.beehiiv.com/subscribe