{"id":654,"hash":"51334f7a2151566eaa8e546dbf016d23190f38195572b18b5df158380e582d3b","pattern":"Absolute paths (baseUrl) gives error: Cannot find module","full_message":"I am setting a configuration to run my tests in a create-react-app + typescript app (from which I have ejected). I am using jest + enzyme. In my tsconfig.json I have set baseUrl='./src' so I can use absolute paths when I import modules. For example this is a typical import statement in one of my files:\n\nimport LayoutFlexBoxItem from 'framework/components/ui/LayoutFlexBoxItem';\n\nYou can see that the path is absolute (from /src folder) and not relative. \nThis works fine when I run in debug mode ( yarn start )\n\nBut when I run my test ( yarn test ), I get this error:\n\n Cannot find module 'framework/components/Navigation' from 'index.tsx'\n\nSo it looks like jest is not able to resolve this absolute path although I have set it up in my tsconfig.json. This is my tsconfig.json:\n\n{\n  \"compilerOptions\": {\n    \"outDir\": \"dist\",\n    \"module\": \"esnext\",\n    \"target\": \"es5\",\n    \"lib\": [\"es6\", \"dom\"],\n    \"sourceMap\": true,\n    \"allowJs\": true,\n    \"jsx\": \"react\",\n    \"moduleResolution\": \"node\",\n    \"rootDir\": \"src\",\n    \"forceConsistentCasingInFileNames\": true,\n    \"noImplicitReturns\": true,\n    \"noImplicitThis\": true,\n    \"noImplicitAny\": true,\n    \"strictNullChecks\": true,\n    \"suppressImplicitAnyIndexErrors\": true,\n    \"noUnusedLocals\": true,\n    \"baseUrl\": \"./src\"    \n  },\n  \"exclude\": [\n    \"node_modules\",\n    \"build\",\n    \"dist\",\n    \"config\",    \n    \"scripts\",\n    \"acceptance-tests\",\n    \"webpack\",\n    \"jest\",\n    \"src/setupTests.ts\"\n  ]\n}\n\nNow I can see that there is a generated tsconfig.test.json at the root of my project. This is the ts configuration used for test. And here is its content:\n\n{\n  \"extends\": \"./tsconfig.json\",\n  \"compilerOptions\": {\n    \"module\": \"commonjs\"\n  }\n}\n\nAs you can see the \"module\" is commonjs here whereas in the default configuration it is esnext. Could this be one reason?\n\nHas any one been able to unit test his typescript project with Jest and absolute path? or is this a known bug? Since I have ejected from default configuration, are there some settings to put in my webpack configuration?\n\nThanks for your input and suggestion.","ecosystem":"npm","package_name":"typescript","package_version":null,"solution":"I was struggling with the same problem and actually it turns out that a simple change seems to do the trick.\n\nI just updated the moduleDirectories field in jest.config.js.\n\nBefore\n\nmoduleDirectories: ['node_modules']\n\nAfter\n\nmoduleDirectories: ['node_modules', 'src']","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/50171412/absolute-paths-baseurl-gives-error-cannot-find-module","votes":200,"created_at":"2026-04-19T04:51:27.261087+00:00","updated_at":"2026-04-19T04:51:27.261087+00:00"}