The user defined paths in tsconfig.json are not found – TypesScript says … I have reinitialized the whole project. Used stock ts configs, checked every setting, updated everything. It just don't want to work.
Project structure:
|-./
|--index.ts
|--package.json
|--tsconfig.json
|--database/
|---dbOperations/
|----index.ts
|--api/
|---server.ts
package.json:
{
"name": "name",
"version": "0.1.0",
"private": true,
"description": "desc.",
"main": "index.ts ",
"scripts": {
"test": "jest --config jest.config.json",
"tsnode": "ts-node index.ts",
"lint": "eslint '**/*.ts' --ignore-pattern node_modules/",
},
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.15.0",
"@babel/preset-env": "^7.15.0",
"@babel/preset-typescript": "^7.15.0",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/jest": "^27.0.1",
"@typescript-eslint/eslint-plugin": "^4.32.0",
"@typescript-eslint/parser": "^4.32.0",
"eslint": "^7.32.0",
"jest": "^27.1.0",
"nodemon": "^2.0.12",
"ts-jest": "^27.0.5",
"ts-node": "^10.1.0",
"typescript": "^4.3.5"
},
"dependencies": {
"express": "^4.17.1",
}
}
tsconfig.json (initial config after tsc --init with paths):
{
"compilerOptions": {
"strict": true,
"target": "es5",
"module": "commonjs",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"paths": {
"@app/*": [
"./*"
],
"@api/*": [
"api/*"
],
"@database/*": [
"database/*"
],
"@dbOperations": [
"database/dbOperations/index.ts"
]
},
}
}
When I do import {x, y, z} from "@dbOperations"
in /api/server.ts
it won't work.
tsnode throws:
Error: Cannot find module '@dbOperations'
Any suggestions?
CodePudding user response:
TypeScript only uses paths defined in tsconfig.json
when compiling, but it doesn't actually output valid paths that are understandable for Node. You have to install tsconfig-paths to make it work.