dynamically load parts of an application at runtime.
Questions tagged [dynamic-import]
360 questions
119
votes
9 answers
Dynamically import a method in a file, from a string
I have a string, say: abc.def.ghi.jkl.myfile.mymethod. How do I dynamically import mymethod?
Here is how I went about it:
def get_method_from_file(full_path):
if len(full_path) == 1:
return map(__import__,[full_path[0]])[0]
return…

lprsd
- 84,407
- 47
- 135
- 168
69
votes
1 answer
Using the dynamic import() function on Node.js
I'm trying to implement basic functionality of a dynamic import inside a Node v10 environment.
main.js
async function main() {
try {
const moduleA = await import('./moduleA');
console.log('Inside main function...');
moduleA();
…

cbdeveloper
- 27,898
- 37
- 155
- 336
31
votes
4 answers
Dynamically importing Python module
I have a trusted remote server that stores many custom Python modules. I can fetch them via HTTP (e.g. using urllib2.urlopen) as text/plain, but I cannot save the fetched module code to the local hard disk. How can I import the code as a fully…

dpq
- 9,028
- 10
- 49
- 69
25
votes
1 answer
How do I make dynamic imports work with Jest?
I'm writing unit tests for my application (Vue.js) and I'm having troubles while trying to test a method that imports a module dynamically. I wanted to mock the library (file-saver) to test that it is called correctly but instead all I get is an…

Sacha Morgese
- 251
- 4
- 6
23
votes
6 answers
How to prevent typescript from transpiling dynamic imports into require()?
I'm building a discord.js Discord bot. Now for some reason, discord.js doesn't work with ESM modules (a totally separate issue), so my bot app uses CommonJS modules. Now I have another project on my system called Lib, which has a lot of utility…

The Freeze
- 271
- 2
- 5
20
votes
3 answers
React Router with React 16.6 Suspense "Invalid prop `component` of type `object` supplied to `Route`, expected `function`."
I'm using the latest version (16.6) of React with react-router (4.3.1) and trying to use code splitting using React.Suspense.
Although my routing is working and the code did split into several bundles loaded dynamically, I'm getting a warning about…

ronnyrr
- 1,481
- 3
- 26
- 45
20
votes
1 answer
Dynamic imports in ES6 with runtime variables
Recently stumbled upon the dynamic import proposal and also this Youtube video . Thought would be a great idea to use it for on demand imports of components in React.
Running into an issue where I was not able to "resolve" a path when import is…

semuzaboi
- 4,972
- 5
- 20
- 35
19
votes
1 answer
Name webpack chunks from react-loadable
I've successfully added react-loadable library in my project to enable code splitting, the only problem I've found is that the chunks generated by webpack are not named, they are given integer names.
My code for react-loadable use is
const…

Khriz
- 5,888
- 6
- 34
- 39
17
votes
5 answers
Dynamic Importing of an unknown component - NextJs
I want to load a component dynamically based on the route. I'm trying to make a single page which can load any individual component for testing purposes.
However whenever I try to do import(path) it shows the loader but never actually loads. If I…

justin.m.chase
- 13,061
- 8
- 52
- 100
17
votes
1 answer
Dynamic loading of external modules in webpack fails
I am trying to set up the following architecture: a core React application that gets built with some basic functionality, and the ability to load additional React components at runtime. These additional React components can be loaded on-demand, and…

SlyCaptainFlint
- 501
- 1
- 4
- 12
17
votes
2 answers
Python - create an EXE that runs code as written, not as it was when compiled
I'm making a pygame program that is designed to be modular. I am building an exe with pygame2exe of the file main.py, which basically just imports the real main game and runs it. What I'm hoping for is a sort of launcher that will execute Python…

Matthew Fournier
- 1,077
- 2
- 17
- 32
15
votes
2 answers
How to test dynamic imports (then and catch) with Jest
So I have been struggling with how to test dynamic imports generally and in this case, especially in jest, I have been reading a lot on the internet but did not find anything concrete, so I thought about bringing the question up to centralize a…

Enmanuel Duran
- 4,988
- 3
- 17
- 29
15
votes
4 answers
How do I dynamically import images in React?
Have seen a couple of answers online but there are no clear explanations and the solutions don't work. So this is what I am trying to do:
I have a folder of MANY images (thousands) - currently it is saved under src/assets/images folder
Given a list…

Daryll
- 503
- 1
- 9
- 15
15
votes
1 answer
Garbage collect unused modules
I am using dynamic import to load scripts written by the user in the browser. I start by placing the script contents into a blob, then I use dynamic import() to load the script as a module. Over time, I expect these scripts to change and be…

James Aguilar
- 383
- 1
- 7
13
votes
1 answer
Awaiting on dynamic imports in JavaScript
While trying to port some of our JS code to use modules, I ran into this peculiar case which I couldn't explain. I was setting up my main JS file to have dynamic imports for my main entry points and they in turn have imports for all the files they…

Shivaprasad
- 399
- 2
- 5
- 12