Paths not being resolved. Cannot find module X from Y
See original GitHub issueIssue:
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:
- Created 6 years ago
- Reactions:28
- Comments:12 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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:To make it clear for others this solution worked:
And make sure the
tsconfig.json
file has no comments or extra commas.