Error When Including Module From Mapped Path
See original GitHub issueHi there, I’m getting an error when trying to load a module from a mapped path. The error is as follows:
Cannot find module '@app/constants/actionTypes' from 'index.ts'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:151:17)
at Object.<anonymous> (src/actions/auth/index.ts:3:69)
at Object.<anonymous> (src/actions/auth/spec.ts:4:15)
And I’m trying to do the following:
import * as actionTypes from "@app/constants/actionTypes";
The file definitely exists and worked with relative paths.
Here’s my tsconfig.json
:
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"jsx": "react",
"baseUrl": ".",
"paths": {
"@app/*": [ "./src/*" ]
}
},
"filesGlob": [
"src/**/*.ts",
"src/**/*.tsx",
"src/*.ts",
"src/*.tsx"
],
"exclude": [
"node_modules"
]
}
And here’s the jest
configuration of package.json
;
"jest": {
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"transform": {
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testResultsProcessor": "<rootDir>/node_modules/ts-jest/coverageprocessor.js",
"testRegex": "(/test/.*|(test|spec))\\.(ts|tsx)$",
"collectCoverage": true,
"collectCoverageFrom": [
"src/**/*.{ts,tsx}"
],
"moduleDirectories": [
"node_modules",
"src"
]
}
As you can see, I’m including the moduleDirectories
, as suggested.
What’s weirder still, is that I can still reference other modules through that path, and the following runs without error:
import { RequestLogin, ResponseError, ResponseAuth } from "@app/models/api";
Compiling directly with tsc
and with the --traceResolution
flag it seems to resolve without issue:
======== Resolving module '@app/constants/actionTypes' from '/var/www/front-end/src/actions/auth/index.ts'. ========
'baseUrl' option is set to '/var/www/front-end', using this value to resolve non-relative module name '@app/constants/actionTypes'
'paths' option is specified, looking for a pattern to match module name '@app/constants/actionTypes'.
Module name '@app/constants/actionTypes', matched pattern '@app/*'.
Trying substitution './src/*', candidate module location: './src/constants/actionTypes'.
Loading module as file / folder, candidate module location '/var/www/front-end/src/constants/actionTypes'.
File '/var/www/front-end/src/constants/actionTypes.ts' exist - use it as a name resolution result.
Resolving real path for '/var/www/front-end/src/constants/actionTypes.ts', result '/var/www/front-end/src/constants/actionTypes.ts'
======== Module name '@app/constants/actionTypes' was successfully resolved to '/var/www/front-end/src/constants/actionTypes.ts'. ========
It’s baffling me as I can’t work out why it would affect one module, but not the other.
Thanks for your help.
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Jest Path Mapping Error: Could not locate module xyz ...
I could imagine that this error is related to my project folder structure and ts config. It maps @app/* to ./* (as shown...
Read more >Documentation - Module Resolution - TypeScript
How TypeScript resolves modules in JavaScript. ... import can be resolved relative to baseUrl , or through path mapping, which we'll cover below....
Read more >Go Modules Reference - The Go Programming Language
See Mapping versions to commits and Module directories within a repository. If the module is released at major version 2 or higher, the...
Read more >redefinition of module error since Xcode 8.3 - Apple Developer
Since I updated to xcode 8.3 I can't compile my project, it was compiling fine with the previous version of xcode. It contains...
Read more >Invalid File Path When Importing Targets Error - SugarClub
In the mean time we are just fixing the import file headings to they map ... file path in /var/www/html/sugar/modules/Import/sources/ImportFile.php:103
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
@archy-bold add the following to the
jest
section in yourpackage.json
This should solve the issue
Hi, by setting the
src
folder inmoduleDirectories
just worked fine for me.