Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
KarmaBlackshaw opened this issue Aug 2, 2022 · 6 comments
Open

Comments

@KarmaBlackshaw
Copy link

KarmaBlackshaw commented Aug 2, 2022

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

@DeeSouza
Copy link

DeeSouza commented Aug 17, 2022

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?

@KarmaBlackshaw
Copy link
Author

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

@mhm13dev
Copy link

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 🙏

@Vikmanatus
Copy link

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

@girixcode7
Copy link

girixcode7 commented Mar 25, 2023

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"]
}

@sachin-kiwi
Copy link

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"
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants