{"id":515,"hash":"78b81c26002c0770dd0398afa9941054978bbfbfd33c814d5991f6924266f147","pattern":"NestJS Jest cannot find module with absolute path","full_message":"I have a quite new NestJS application. I'm trying to run unit tests, but they keep failing due to 'cannot find module..' when using absolute paths (\"src/users/...\"), but works when using relative paths (\"./users/..\"). Is there anything wrong with my configuration here?\n\nJest setup in package.json:\n\n\"jest\": {\n  \"moduleFileExtensions\": [\n    \"js\",\n    \"json\",\n    \"ts\"\n  ],\n  \"rootDir\": \"src\",\n  \"testRegex\": \".spec.ts$\",\n  \"transform\": {\n    \"^.+\\\\.(t|j)s$\": \"ts-jest\"\n  },\n  \"coverageDirectory\": \"../coverage\",\n  \"testEnvironment\": \"node\"\n}\n\ntsconfig.json:\n\n{\n  \"compilerOptions\": {\n    \"module\": \"commonjs\",\n    \"declaration\": true,\n    \"removeComments\": true,\n    \"emitDecoratorMetadata\": true,\n    \"experimentalDecorators\": true,\n    \"allowSyntheticDefaultImports\": true,\n    \"target\": \"es2017\",\n    \"sourceMap\": true,\n    \"outDir\": \"./dist\",\n    \"baseUrl\": \"./\",\n    \"incremental\": true\n  }\n}","ecosystem":"npm","package_name":"node.js","package_version":null,"solution":"I had the same issue, the problem was the default jest configuration created by Nestjs.\n\nI changed \"rootDir\": \"src\" to \"rootDir\": \"./\" and add \"modulePaths\": ['<rootDir>'].\n\nFinaly, my jest configuration looks like this:\n\n  moduleFileExtensions: ['js', 'json', 'ts'],\n  rootDir: './',\n  modulePaths: ['<rootDir>'],\n  testRegex: 'spec.ts$',\n  transform: {\n    '^.+\\\\.(t|j)s$': 'ts-jest'\n  },\n  coverageDirectory: './coverage',\n  testEnvironment: 'node',\n\nIf you have some relative paths to your config you will probably have to update them because your rootDir is not src anymore.\n\nYou can even remove rootDir is you setup the jest config in package.json or if the config file is located at the root of your project, as explained in the doc: https://jestjs.io/docs/en/configuration#rootdir-string\n\nAnd if you want read about modulePaths: https://jestjs.io/docs/en/configuration#modulepaths-arraystring\n\nHope it will also work for you.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/64028100/nestjs-jest-cannot-find-module-with-absolute-path","votes":27,"created_at":"2026-04-19T04:51:16.281630+00:00","updated_at":"2026-04-19T04:51:16.281630+00:00"}