Intellisense for module-alias Package

1.6k views Asked by At

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?

1

There are 1 answers

0
relief.melone On BEST ANSWER

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