1

I would like to import modules using Typescript path aliases. Now I am importing as follows.

  • user.controller.ts
import { resourcePermissions } from '../../utils/resource-permissions';
import { allRoleAuthDetails } from '../../utils/autherize-details';

I would like to change is as follows

import { resourcePermissions } from '@fboutil/resource-permissions';
import { allRoleAuthDetails } from '@fboutil/autherize-details';

I updated my tsconfig.json file as follows

  • tsconfig.json
{
  "$schema": "http://json.schemastore.org/tsconfig",
  "extends": "@loopback/build/config/tsconfig.common.json",
  "compilerOptions": {
    "rootDir": "./",
    "module": "commonjs",
    "noImplicitAny": true,
    "target": "es2017",
    "outDir": "dist",
    "sourceMap": true,
    "baseUrl": ".",
    "moduleResolution": "node",
    "removeComments": true,
    "typeRoots": [
      "node_modules/@types"
    ],
    "paths": {
      "@fboutil/*": [
        "src/utils/*"
      ],
      "*": [
        "node_modules/@types/*",
        "src/types/*"
      ]
    }
  },
  "include": [
    "src"
  ]
}

The code doesn't show any error in VSCode editor. Also I can navigate to the module files by CNTRL+Click on the import line. But while running the application, it throws error as follows

Cannot start the application. Error: Cannot find module '@fbosutil/autherize-details'
Require stack:
- ......./dist/controllers/user.controller.js
- ......./node_modules/@loopback/boot/dist/booters/booter-utils.js
- ......./node_modules/@loopback/boot/dist/booters/base-artifact.booter.js
- ......./node_modules/@loopback/boot/dist/booters/index.js
- ......./node_modules/@loopback/boot/dist/boot.component.js
- ......./node_modules/@loopback/boot/dist/index.js
- ......./dist/application.js
- ......./dist/server.js
- ......./dist/index.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:94:18)

The same works with express projects. When I use loopback-4 it throws the above error. Please help to solve the issue.

Prince Francis
  • 2,995
  • 1
  • 14
  • 22

1 Answers1

0

But if you run your app with "node dist/index.js" you may see an error looks like this Error: Cannot find module ‘@modules/example/model’ In that case you have to replace all relative path in your build with tsc-alias

You can find more details here : https://nextjsvietnam.com/post/how-to-use-typescript-path-alias-in-loopback-4/

gmmiso88
  • 17
  • 1