question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Paths not being resolved. Cannot find module X from Y

See original GitHub issue

Issue: Doesn’t seem to work with tsconfig.json -> compilerOptions.paths.

Expected behaviour: I would like ts-jest to handle the relationship to files as specified with tsconfig.json -> compilerOptions.paths.

Link to a minimal repo that reproduces this issue: It’s closed source, but what I can do is give you an example.

tsconfig.json

{
	"compiledOptions": {
		"emitDecoratorMetadata": true,
		"experimentalDecorators": true,
		"module": "es2015",
		"moduleResolution": "node",
		"target": "es2017",
		"lib": ["dom", "es2015", "es2016", "es2017"],
		"paths": {
			"@fooBar/*": ["packages/*"]
		}
	}
}

I did pull out a few more things out of there that isn’t relevant to the issue.


my-test.spec.ts

import {calc} from `@fooBar/misc/calc`;

describe('Misc/Calc', () => {
	it('should do stuff', () => {
		expect(calc(1, 2)).toEqual(3);
	});
});

ERROR

Test suite failed to run

    Cannot find module '@fooBar/misc/calc' from 'my-test.spec.ts'
      
      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17)
      at Object.<anonymous> (packages/services/calculate/index.ts:43:121)

Which is odd, because I can tsc just fine and works 100% okay.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:28
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

24reactions
keawadecommented, Jul 26, 2017

@geirsagberg, that fixes the problem for me.

@maraisr, adding this snippet to your package.json should allow you to take your custom name and map it to your actual folder:

"moduleNameMapper": {
  "^@fooBar/(.*)": "<rootDir>/src/barFoo/$1"
},
18reactions
benwindingcommented, Apr 3, 2020

To make it clear for others this solution worked:

const { pathsToModuleNameMapper } = require('ts-jest/utils');
const { compilerOptions } = require('./tsconfig.json');

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {prefix: '<rootDir>/'})
};

And make sure the tsconfig.json file has no comments or extra commas.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest gives `Cannot find module` when importing components ...
My tests that run files that only contain relative paths in the tree run correctly. To Clarify, I'm looking for how to configure...
Read more >
Cannot find module 'path' Error in TypeScript | bobbyhadz
To solve the "Cannot find module path or its corresponding type declarations" error, install the types for node by running the command npm...
Read more >
cannot find module 'path' or its corresponding type ... - You.com
To solve the "Cannot find module `path` or its corresponding type declarations" error, install the types for node by running the command `npm...
Read more >
Path aliases with TypeScript in Node.js - Level Up Coding
For example: Error: Cannot find module '@components/user'. That's because JS can't resolve the modules for the declared path aliases.
Read more >
CommonJS modules | Node.js v19.3.0 Documentation
require.resolve.paths(request). The module ... Then Node.js will not bother looking for missing dependencies in ... require(X) from module at path Y 1.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found