Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find module that is defined in tsconfig `paths`

I am trying to setup aliases for my mock server. Whenever I try to compile ts files, it returns error that it couldn't find proper modules even though those are defined in tsconfig,json->paths

Folder structure:

├── server
│   └── src
│       └──/json
├── src
│   └──/modules
├── tsconfig.json

Here is my tsconfig.json

{
    "compilerOptions": {
        "baseUrl": "./src",
        "experimentalDecorators": true,
        "jsx": "react",
        "lib": [
            "dom",
            "es2015",
            "es2015.promise"
        ],
        "module": "commonjs",
        "moduleResolution": "node",
        "noImplicitAny": true,
        "noUnusedLocals": true,
        "esModuleInterop": true,
        "paths": {
            "@project/app/modules/*": [
                "modules/*"
            ],
            "@project/server/data/*": [
                "../server/src/json/*"
            ]
        },
        "sourceMap": true,
        "target": "es5"
    },
    "exclude": [
        "node_modules",
        "tools"
    ]
}

Error: Error: Cannot find module '@project/server/data/accounts/accountsList'

like image 643
Jamil Alisgenderov Avatar asked Jun 18 '19 13:06

Jamil Alisgenderov


People also ask

Can not find module path?

To solve the "Cannot find module path or its corresponding type declarations" error, install the types for node by running the command npm i -D @types/node . You can then import path with the following line of code import * as path from 'path' .

What is module in Tsconfig?

Sets the module system for the program. See the Modules reference page for more information. You very likely want "CommonJS" for node projects. Changing module affects moduleResolution which also has a reference page.

Where is my Tsconfig file?

The tsconfig. json is generally put in the root folder of the project.

Can not find module TS?

The "Cannot find module or its corresponding type declarations" error occurs when TypeScript cannot locate a third-party or local module in our project. To solve the error, make sure to install the module and try setting moduleResolution to node in your tsconfig. json file.

What does the tsconfig module do?

TSConfig module. Sets the module system for the program. See the Modules reference page for more information. You very likely want "CommonJS" for node projects. Changing module affects moduleResolution which also has a reference page. Here’s some example output for this file: ts // @filename: index.ts.

How to fix ‘cannot find module’ error in typescript?

To fix the ‘Cannot find module’ error for paths that are in TypeScript tsconfig.json, we need to add all the paths that we want the TypeScript compiler to pick up. How to generate a tsconfig.json file with TypeScript?

How to check if a tsconfig file has been changed?

Check angular.json file if its "build" -> "options" config has "tsConfig" option set as your changed tsconfig file.

Are the paths in tsconfig respected from TS-jest?

Although it does make sense that the paths in tsconfig are respected automatically from ts-jest. I used the moduleNameMapper jest config property and set the same paths. You could try that as a workaround for the time being?


3 Answers

I faced the same issue. I tried many things and now i got a solution which works for me. I have an app and a library in my angular project. I want to use a library alias in my app.

I have following structure:

├── projects
│   └── lib
│       └── src
│           └── lib
│               └── lib-service.ts
│           └── index.ts
│   └── app
│       └──tsconfig.app.json
├── tsconfig.json

In the tsconfig.json file in the root folder I have following paths defined:

"paths": {
  "my-lib": [
    "projects/lib/src/index.ts"
  ]
}

There I access an index.ts file where I define the things I want to export. In my case the index.ts file has the following entry:

export * from './lib/lib-service';

Now I can access the LibService in components of the app with the help of the alias:

import {LibService} from 'my-lib';

It is important to mention that this soloution don't work if I add this entry to the tsconfig.app.json file. I think the IDE (in my case WebStorm) searches for aliases in the tsconfig.json file which is close to the root folder. So I extend the tsconfig.json in my tsconfig.app.json

"extends": "../../tsconfig",

Maybe you have to restart you IDE to remove the red underline of the import line.

I hope this solution works for you. You have to to a little bit more of setup work because you have to define the classes you want to export in the index.ts file. But in case of using libraries it make sense because you don't want to make everything visible for other app.

like image 74
troYman Avatar answered Oct 16 '22 17:10

troYman


After battling it for some time I figured it out you need to include all directories using tsconfig-paths in your main tsconfig.json. Working version of your tsconfig.json file could be

{
    "compilerOptions": {
        "baseUrl": ".",
...
        "paths": {
            "@project/app/modules/*": [
                "src/modules/*"
            ],
            "@project/server/data/*": [
                "server/src/json/*"
            ]
        },
    },

    "include": [
        "./src",
        "./server"
    ],
}
like image 27
Konrad Gałęzowski Avatar answered Oct 16 '22 19:10

Konrad Gałęzowski


My issue was that my tsconfig.app.json which extended my tsconfig.json was overriding the "baseUrl" option incorrectly. I removed it from the tsconfig.app.json so it did not have a "baseUrl" or "paths". In the end, my tsconfig.json had the following compiler options:

"baseUrl": "src/",
"paths": {
    "@shared/*": ["shared/*"],
    "@client/*": ["client/*"],
    "@server/*": ["server/*"]
}
like image 10
Jake Tyler Avatar answered Oct 16 '22 18:10

Jake Tyler