E2020 dynamic imports in JavaScript

Escmascript Is a standard from which JavaScript and typescript are both derived from.

ES2020 is out it comes with it new ways of writing JavaScript code.

Dynamic imports enable importing dependencies in a rather dynamic way.

Initially JavaScript developers were restricted to statically load their dependencies for instance;

import { property } from 'dependency'

On runtime the JavaScript engine loads the required dependencies and makes them accessible globally.

However that is a rigid way of writing code.

Dynamic imports are  relatively more flexible therefore you can included them at any segment within your program.

For instance if you want to load file system module only if a certain criteria is met you would do as follows;

If(process.argv[2] == 'start')
Import fs from 'fs'

Dynamic imports return a promise use the syntax below to make use of them

.then(fs => {
   // consume module fs


Popular posts from this blog

How to set content disposition header for nginx server to force content download

Animated daylight on HTML5 Canvas

How to make a static http server in nodejs using express

How to make a Snake Game using JavaScript and html5

JS13K Preparation