Intellisense for module-alias Package
Asked Answered
C

1

9

I am using this npm module (module-alias) https://www.npmjs.com/package/module-alias in my Node project.

To make use of this package you have to set path aliases in the package.json file.

package.json

However, using this package comes with the disadvantage, that intellisense doesn't work anymore.

My question is how to enable intellisense with those path aliases?

Cedar answered 9/12, 2018 at 15:12 Comment(1)
Indeed, the question of the year. I wish path aliases were implemented natively in node. It even could grasp the best from webpack regarding to path resolving.Sandusky
C
3

The problem is that you did not register those aliases anywhere with your linter. I would gerenally suggest to use ESLint here (even if you use TypeScript as TSLint will be discontinued in favour of ESLint). My examples will include the TypeScript endings as well. If you definately want to make it work for JavaScript only you can skip the .ts .tsx extentensions in the eslint) So to make intellisense work do this in

.eslintrc.js

settings: {
  "import/resolver": {
    alias : {
      map: [
        ["@","./src"]
      ],
      extensions: [".js", ".jsx", ".ts", ".tsx"],
    },
  }
},

Note that in this case you will need the import Plugin for ESLint. If you don't already have it install it.

If you are using TypeScript you will also have to make that alias known to your compiler. So add this to your

tsconfig.json

"compilerOptions": {
  "baseUrl": ".",
  "paths": [
    "@/*" : [
      "src/*"
    ]
  ]
}
Chipman answered 8/1, 2020 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.