JavaScript Import/Require Example

Sep 24, 2020

Example of JavaScript Module

Import specific export

import { FruitType, Fruit, tell } from './TestModule.js'

Import everythong

import * from './TestModule.js'

Rename import

import { tell as show } from './TestModule.js'

Import As Module

import * as TestModule from './TestModule.js'TestModule.tell('Apple')

or

import TestModule from './TestModule.js'

Dynamic Loading

import('./TestModule.js')  .then((module) => {    module.tell('Apple')  })

or

let TestModule = await import('./TestModule.js')

Import defaults

import { FruitType, Fruit, tell } from './TestModule.js'

or

import myDefault, * as myModule from '/modules/my-module.js'

or

import myDefault, {foo, bar} from '/modules/my-module.js'

or

// dynamic importconst { default: myDefault, foo, bar } = await import('/modules/my-module.js')

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.