Next.js Discord

Discord Forum

Struggling with dynamic import function

Unanswered
Spectacled Caiman posted this in #help-forum
Open in Discord
Avatar
Spectacled CaimanOP
    async function loadModule(file) {
        try {
            // const module = await import('<the path here>'); <- this works
            let fullPath = await getPathIncludingSrc(file)
            let fullPathStr = `${fullPath}` <-- results to EXACTLY the same string
            // const module = await import('src/pages/api/modules/resolvers/Session/createSession'); <- this won't??
            const module = await import(fullPathStr);
            // Assuming you want to push the default export of the module
            self.modules.push(module.default);
        } catch (error) {
            console.error(`Error loading module: ${file}`, error);
        }
    }

Im trying to do dynamic import but its not working if I use a variable as string even though if I pass exactly the same string it works Im so confused

0 Replies