Skip to content

Instantly share code, notes, and snippets.

@felipekm
Created April 13, 2023 17:44
Show Gist options
  • Save felipekm/63ced7f7512380378501d02b36602072 to your computer and use it in GitHub Desktop.
Save felipekm/63ced7f7512380378501d02b36602072 to your computer and use it in GitHub Desktop.
NodeJS - How to fix Intellisense for module-alias Package

If you are not using CommonJS on NodeJS and having problems with the intellisense for getting into modules.

  1. Install module-alias and module-alias-jest
  2. Configure your package.json file with your aliases:
{
  "_moduleAliases": {
    "$root": ".",
    "$src": "./src",
  }
}
  1. Add the package your very first index module: require('module-alias/register');

  2. Configure your jest.config file like this below:

const aliases = require('module-alias-jest/register');
const jestConfig = {
  testEnvironment: 'jest-environment-node',
  testEnvironmentOptions: {
    NODE_ENV: 'test'
  },
  coveragePathIgnorePatterns: ['node_modules', 'src/index.js', 'test'],
  coverageReporters: ['text', 'lcov', 'clover', 'html'],
  testMatch: ['**/*.spec.js'],
  restoreMocks: true,
  verbose: true,
  moduleNameMapper: aliases.jest
};

module.exports = jestConfig;
  1. Creates a jsonconfig.json file on your root and place the JSON below:
{
  "compilerOptions": {
      "module": "commonjs",
      "target": "es6",
      "baseUrl": "./",
      "paths": {
          "$root/*": ["./*"],
          "$src/*": ["./src/*"],
          "$services/*": ["./src/services/*"],
          "$clients/*": ["./src/clients/*"],
      }
  },
  "exclude": ["node_modules", "**/node_modules/*"]
}
@amirrezabahramy
Copy link

Thank you. this helped me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment