Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to dynamically import a node module?

I would like to know if it is possible to dynamically import a module installed via npm install.

Something like:

import("{ AsyncStorage } from react-native ???").then((module) => {
  // do something with module
});

All the examples I found so far are just importing self written ES modules...

like image 625
Melkis H. Avatar asked Jun 01 '26 16:06

Melkis H.


1 Answers

You should do it like this

// With normal promises
import('react-native').then(({ AsyncStorage }) => {
 // Do something with the module
})

// Async/await (You must add this code inside of an async function or use ecmascript module code)
const { AsyncStorage } = await import('react-native')
// Do something with the module
like image 169
AngelNext Avatar answered Jun 04 '26 09:06

AngelNext



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!