How to Make VSCode Recognize Your Local Package?

Bairui Su,web development

screen

Assuming that you are developing an NPM package named echox, and you export a method from src/index.js.

// ./src/index.js
export function html() {
  // ...
}

Then you import it in test/index.spec.js.

// ./tests/index.spec.js
import * as X from 'echox'

Now, the question arises: how to make VSCode recognize your local package? Or more specifically: how to jump to the definition by pressing cmd and click 'echox' in this line?

The solution is to modify exports.default in package.json:

{
  "exports": {
    "default": "./src/index.js"
  }
}

That's it!

tooltip

This works because VSCode attempts to resolve dependencies from package.json and node_modules. When exports.default in package.json is specified, VSCode will import methods from ./src/index.js wherever ehcox is imported without specifying a subpath.

You can find the complete code in this PR (#55 (opens in a new tab)). If you like it, please star EchoX (opens in a new tab) in Github!


© Bairui Su.RSS