Importing the Sourcegraph API module

In order to use the Sourcegraph extension API, your extension needs to importthe sourcegraph module at the top of the file.

This import will look like this:

import * as sourcegraph from 'sourcegraph'

Or, with the equivalent require syntax:

const sourcegraph = require('sourcegraph')

How the import works

Unlike a regular module that is installed with npm or yarn and is packagedor bundled with your project, the sourcegraph module is actually injecteddynamically when your extension is loaded within a Sourcegraph instance.

The sourcegraph module that you install as a package.json dependency isactually a set of TypeScript type definitions that allow you to use theSourcegraph API's interfaces. The module doesn't contain the implementations ofthese interfaces.


Because the sourcegraph module doesn't actually contain any implementationcode, a separate helper module is provided to create stubs of the API fortesting:@sourcegraph/extension-api-stubs.

See Testing extensions for more about writing automatedtests for your extension.