#invalid module specifier error when using dynamic imports

5 messages · Page 1 of 1 (latest)

wraith merlin

i am importing modules like so:

const fileModule: Module = await import(filePath);

this results in a ERR_INVALID_MODULE_SPECIFIER:

TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "dist\commands\purge.js" is not a valid package name imported from REDACTED
    at parsePackageName (node:internal/modules/esm/resolve:772:11)
    at packageResolve (node:internal/modules/esm/resolve:795:5)
    at moduleResolve (node:internal/modules/esm/resolve:901:20)
    at defaultResolve (node:internal/modules/esm/resolve:1121:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
    at ModuleLoader.import (node:internal/modules/esm/loader:328:34)
    at importModuleDynamically (node:internal/modules/esm/translators:146:35)
    at importModuleDynamicallyCallback (node:internal/modules/esm/utils:176:14)

i can't seem to be able to understand the reason for this as the official mdn docs show the correct usage as essentially the same thing.

rustic depotBOT
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
  • Marked as resolved by OP
wary merlin

If it’s supposed to be a relative path it needs to start with ./ or ../, if it’s absolute it needs to start with / or file://
If it doesn’t start with either NodeJS will look for a package by that name in node_modules

wraith merlin
wary merlin