Importing scoped npm modules without scope name in TypeScript

Viewed 948

We have some private NPM packages using npmjs' private repository. Assume

  • scope name : @scope
  • private package name as : private-package

When we install this NPM package using npm install @scope/private-package

It is installed to : .node_modules/@scope/private-package

In VSCode, to import some thing from this package, we need to do import {SomeThing} from '@scope/private-package'

Is there anything in TypeScript configuration that we can re-map @scope/private-package to private-package so that we can use like import {SomeThing} from 'private-package' instead of adding@scope?

2 Answers

You can use tsconfig's path mapping feature for this. in the "compilerOptions" section, add a "paths" section with an entry for each path map. For instance, in your case:

{
  "compilerOptions": {
    ...
    "paths": {
      "private-package": ["@scope/private-package"]
    }
  }
}

Note that if you use a bundler, it won't be aware of this mapping and you might have to configure it also.

Related