How to specify mapping alias for imports in a Node.js typescript project?

19 Views Asked by At

I have specified a mapping in the tsconfig.json file to import shared code -

"paths": {
    "@shared/*": [
        "../shared/ts/*"
    ],
}

Complete user-service/tsconfig.json file:

{
    "compilerOptions": {
        "target": "es5",
        "lib": [
            "es6"
        ],
        "baseUrl": "./",
        "paths": {
            "@shared/*": [
                "../shared/ts/*"
            ],
        },
        "module": "commonjs",
        // "rootDir": "./",
        "resolveJsonModule": true,
        "allowJs": true,
        "outDir": "build",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "noImplicitAny": true,
        "skipLibCheck": true
    }
}

Folder structure of the project:

.
├── README.md
├── docker-compose-dev.yaml
├── shared
│   └── ts
│       ├── constants
│       │   ├── errorMessages.ts
│       │   └── statusMessages.ts <- #1
│       ├── package-lock.json
│       ├── package.json
│       ├── tsconfig.json
│       └── types
│           ├── configTypes.ts <- #2
│           └── sm.ts
├── start-dev.sh
└── user-service
    ├── Dockerfile.dev
    ├── Dockerfile.prod
    ├── build
    │   └── index.js
    ├── nodemon.json
    ├── package-lock.json
    ├── package.json
    ├── src
    │   ├── config
    │   │   ├── config.ts
    │   │   ├── database.ts
    │   │   └── env.ts
    │   ├── dev.env
    │   └── index.ts <- #3
    └── tsconfig.json

But inside index.ts [#3], when I try to import a the file from shared/constants/statusMessages [#1], this shows error. But importing shared/types/configTypes [#2] works.

shared/tsconfig.json file:

{
    "compilerOptions": {
        "target": "es5",
        "lib": [
            "es6"
        ],
        "composite": true,
        "esModuleInterop": true
    }
}

index.ts file:

import express from 'express';
import ENV from './config/env';
import config from './config/config';

import { EnvConfig } from '@shared/types/configTypes'; // This import is working
import statusMessages from '@shared/constants/statusMessages'; // This import is not working
// import statusMessages from '../../shared/ts/constants/statusMessages';

const LOG = config[ENV.NODE_ENV].log();

const app = express();



LOG.info("Hello from user-service!");
LOG.info(statusMessages.STATUS_SUCCESS);

const PORT: number = 80;

app.get('/', (req, res) => {
    res.send('Hello World!');
});

app.listen(PORT, () => {
    LOG.info(`Server is running on port ${PORT}`);
})

Error thrown:

Error: Cannot find module '@shared/constants/statusMessages'
Require stack:

start script:

"start:dev": "nodemon --legacy-watch ./src/index.ts | \"./node_modules/.bin/bunyan\""

Console output:

$ npm run start:dev

> [email protected] start:dev
> nodemon --legacy-watch ./src/index.ts | "./node_modules/.bin/bunyan"

[nodemon] 3.1.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: ts,js,json
[nodemon] starting `ts-node ./src/index.ts`
Error: Cannot find module '@shared/constants/statusMessages'

However the relative path import, that is commented out is working.

How to fix the alias import error? Why this error is coming only from the single import, not the other?

If I miss to provide any information, please comment.

I tried to use relative path for import, this worked. But I want to use alias import.

0

There are 0 best solutions below