{"id":652,"hash":"411d9d2f00a02739c5d5e441f474bf4fdab1da1337ba5f1f191dfeaaa563e5b5","pattern":"Jest gives `Cannot find module` when importing components with absolute paths","full_message":"Receiving the following error when running Jest\n\nCannot find module 'src/views/app' from 'index.jsx'\n\n  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17)\n  at Object.<anonymous> (src/index.jsx:4:12)\n\nindex.jsx \n\nimport AppContainer from 'src/views/app';\n\npackage.json\n\n  \"jest\": {\n    \"collectCoverageFrom\": [\n      \"src/**/*.{js,jsx,mjs}\"\n    ],\n    \"setupFiles\": [\n      \"<rootDir>/config/polyfills.js\"\n    ],\n    \"testMatch\": [\n      \"<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}\",\n      \"<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}\"\n    ],\n    \"testEnvironment\": \"node\",\n    \"testURL\": \"http://localhost\",\n    \"transform\": {\n      \"^.+\\\\.(js|jsx|mjs)$\": \"<rootDir>/node_modules/babel-jest\",\n      \"^.+\\\\.css$\": \"<rootDir>/config/jest/cssTransform.js\",\n      \"^(?!.*\\\\.(js|jsx|mjs|css|json)$)\": \"<rootDir>/config/jest/fileTransform.js\"\n    },\n    \"transformIgnorePatterns\": [\n      \"[/\\\\\\\\]node_modules[/\\\\\\\\].+\\\\.(js|jsx|mjs)$\"\n    ],\n    \"moduleDirectories\": [\n        \"node_modules\",\n        \"src\"\n    ],\n    \"moduleNameMapper\": {\n      \"^react-native$\": \"react-native-web\"\n    },\n    \"moduleFileExtensions\": [\n      \"web.js\",\n      \"js\",\n      \"json\",\n      \"web.jsx\",\n      \"jsx\",\n      \"node\",\n      \"mjs\"\n    ]\n  },\n\nMy tests that run files that only contain relative paths in the tree run correctly.\n\nTo Clarify, I'm looking for how to configure Jest to not fail on absolute paths.","ecosystem":"npm","package_name":"node.js","package_version":null,"solution":"In package.json you have the below lines of Jest configuration:\n\n\"moduleDirectories\": [\n    \"node_modules\",\n    \"src\"\n]\n\nThese says that each module you import will be searched for first in node_modules, then in src.\n\nSince the code is already looking in the src/, you should use:\n\nimport AppContainer from 'views/app';\n\nPlease note that this path is absolute to the src directory, you do not have to navigate to locate it as relative path.\n\nOr you can configure your root directory in moduleDirectories inside your Jest configuration (in your case, within package.json) so that all your components could be easily found.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/50863312/jest-gives-cannot-find-module-when-importing-components-with-absolute-paths","votes":206,"created_at":"2026-04-19T04:51:27.259753+00:00","updated_at":"2026-04-19T04:51:27.259753+00:00"}