Jest gives `Cannot find module` when importing components with absolute paths

I think you’re looking for: roots or modulePaths and moduleDirectories

You can add both relative and absolute paths.

I would make sure to include <rootDir> in the roots array, <rootDir> in the modulePaths array, and node_modules in the moduleDirectories array, unless you’ve got a good reason to exclude them.

"jest": {
  "roots": [
    "<rootDir>",
    "/home/some/path/"
  ],
  "modulePaths": [
    "<rootDir>",
    "/home/some/other/path"
  ],
  "moduleDirectories": [
    "node_modules"
  ],
}

Leave a Comment