最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Difference between require() and await import() - Stack Overflow

programmeradmin0浏览0评论

I would like to know the difference between require(x) and await import(x) in terms of code splitting and lazy loading. Are they both the same? If yes, then why does await import(x) exist in the first place as one can use require() statements anywhere he wants. Any in depth answer would be highly appreciated.

I would like to know the difference between require(x) and await import(x) in terms of code splitting and lazy loading. Are they both the same? If yes, then why does await import(x) exist in the first place as one can use require() statements anywhere he wants. Any in depth answer would be highly appreciated.

Share Improve this question asked Feb 3, 2021 at 7:31 KMA BadshahKMA Badshah 1,12510 silver badges19 bronze badges 2
  • require is still used when you don't use Babel transpiller. Reason is that import, export keywords were added to JS in ES6 and async/await was added in ES8. This is why you see required modules in Node.js but imports in React for ex. – Dominik Matis Commented Feb 3, 2021 at 7:35
  • require is CommonJS specific syntax that is supported by Node, but not by browsers. I predates ES modules. ES modules are standardized now and supported in more and more environments. – Felix Kling Commented Feb 3, 2021 at 8:20
Add a ment  | 

2 Answers 2

Reset to default 3

They are not the same. I think you should read this:

https://medium./puted-parisons/monjs-vs-amd-vs-requirejs-vs-es6-modules-2e814b114a0b

import is when requiring an es module, that's the new ecma script standard, it has many benefits over require (mon js modules)

  1. import(x) allows you to selectively load only the items you need, so it can help save memory
  2. import(x) can be run asynchronously, so better performance
发布评论

评论列表(0)

  1. 暂无评论