module-alias icon indicating copy to clipboard operation
module-alias copied to clipboard

A way for VSCode to detect the location of the aliased directory

Open KarmaBlackshaw opened this issue 2 years ago • 6 comments

Using the normal require function gives the developers benefit of hovering over the function then VSCode will be able to show the necessary arguments of the function. Moreover, you can click alt to go directly to the hovered function. Is there a way to do this using this library?

An image using the normal require function image

Requiring a module using module-alias image

KarmaBlackshaw avatar Aug 02 '22 12:08 KarmaBlackshaw

You need to create a file inside of the root project, called jsconfig.json with the following properties:

{
    "compilerOptions": {
      "baseUrl": "./",
      "module": "commonjs",
      "paths": {
        "@shared/*": ["./shared/*"],
        "@modules/*": ["./modules/*"],
        "@config/*": ["./config/*"],
        "@root/*": ["./*"]
      }
    },
    "exclude": ["node_modules"]
  }

Did you already try this one?

DeeSouza avatar Aug 17 '22 12:08 DeeSouza

You need create a file inside of the root project, called jsconfig.json with the nexts properties:

{
    "compilerOptions": {
      "baseUrl": "./",
      "module": "commonjs",
      "paths": {
        "@shared/*": ["./shared/*"],
        "@modules/*": ["./modules/*"],
        "@config/*": ["./config/*"],
        "@root/*": ["./*"]
      }
    },
    "exclude": ["node_modules"]
  }

Did you already try this one?

Wow! you got it! Thank you

KarmaBlackshaw avatar Sep 06 '22 18:09 KarmaBlackshaw

You need to create a file inside of the root project, called jsconfig.json with the following properties:

{
    "compilerOptions": {
      "baseUrl": "./",
      "module": "commonjs",
      "paths": {
        "@shared/*": ["./shared/*"],
        "@modules/*": ["./modules/*"],
        "@config/*": ["./config/*"],
        "@root/*": ["./*"]
      }
    },
    "exclude": ["node_modules"]
  }

Did you already try this one?

Thank you, it was a life saver 🙏

mhm13dev avatar Sep 21 '22 22:09 mhm13dev

Hello to everyone,

If I may, using this config in jsconfig.json may disrupt the default behavior of the eslint VSCode extension (it happened to me last year)

This is my module-alias config in package.json:

  "_moduleAliases": {
    "@": "build/src"
  }

To fix intellisense in VSCode, please follow these steps :

  1. In VSCode extensions, install the extension called Path Intellisense
  2. Then at the root of your project, create a .vscode folder, and inside this folder, create a settings.json file and paste your config.

My vscode config file looks like this:

{
  "path-intellisense.mappings": {
    "@": "${workspaceRoot}/src"
  },
}

You can refer to this repository for a complete example

Best reguards

Vikmanatus avatar Dec 07 '22 18:12 Vikmanatus

You need to create a file inside of the root project, called jsconfig.json with the following properties:

{
    "compilerOptions": {
      "baseUrl": "./",
      "module": "commonjs",
      "paths": {
        "@shared/*": ["./shared/*"],
        "@modules/*": ["./modules/*"],
        "@config/*": ["./config/*"],
        "@root/*": ["./*"]
      }
    },
    "exclude": ["node_modules"]
  }

Did you already try this one?

It did not work for me in Nodejs(18.13.0) Project. Could you please look at this?

node:internal/modules/cjs/loader:1042
  throw err;
  ^

Error: Cannot find module '@routersV1/users/usersRouter'node:internal/modules/cjs/loader:1042
  throw err;
  ^

Error: Cannot find module '@routersV1/users/usersRouter'

package.json

"_moduleAliases": {
    "@root": "./",
    "@utils/*": "./src/utils/*",
    "@controllersV1/*": "./src/controllers/v1/*",
    "@modelsV1/*": "./src/models/v1/*",
    "@routersV1/*": "./src/routers/v1/*",
    "@servicesV1/*": "./src/services/v1/*",
    "@middlewares/*": "./src/middlewares/*"
  }

jsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./",
    "target": "es6",
    "module": "commonjs",
    "allowJs": true,
    "checkJs": true,
    "esModuleInterop": true,

    "resolveJsonModule": true,
    "outDir": "./dist",
    "strict": false,
    "paths": {
      "@/*": ["src/*"],
      "@utils/*": ["src/utils/*"],
      "@controllersV1/*": ["src/controllers/v1/*"],
      "@modelsV1/*": ["src/models/v1/*"],
      "@routersV1/*": ["src/routers/v1/*"],
      "@servicesV1/*": ["src/services/v1/*"],
      "@middlewares/*": ["src/middlewares/*"]
    },
    "lib": ["es6", "dom"]
  },
  "exclude": ["node_modules", "**/node_modules/*", "dist"]
}

girixcode7 avatar Mar 25 '23 21:03 girixcode7

ensure that require('module-alias/register') is run prior to calling any alias directory otherwise you will get error { "name": "your-package", "version": "1.0.0", "scripts": { "start": "node -r module-alias/register src/app.js" }, "dependencies": { "module-alias": "^2.2.2" }, "_moduleAliases": { "@root": ".", "@src": "./src" } }

sachin-kiwi avatar Jul 01 '23 09:07 sachin-kiwi