- “Install”: This command lets you search the npm registry for packages to install, based on what you’ve typed. Selecting a package opens a new shell and invokes
npm install <package name>

- “Scripts”: This command displays scripts in your package.json file. Selecting the script opens a new shell and invokes that command.

- “Uninstall”: This returns all your installed packages. Selecting a package uninstalls it

Setting up your extension replit app
The first thing you want to do is fork an extension template. We recommend using the React Extension Template. although we are not going to write any react code in this tutorial. Add a background script to your extension. You can scaffold a background script by typing inreplkit add background
in the shell. This creates a new folder src/background
. The src/background/main.tsx
file here is where we’ll be writing our code.
Adding a root command
Let’s add a simple root command to the command bar to contain our subcommands.
Building “Uninstall”
Let’s start with Uninstall. This command first figures out what packages you have installed, and then runsnpm uninstall ${package}
The simplest way to figure out what you have installed is by parsing package.json
, and looking at the dependencies object. Since this tutorial is focused on commands, here’s the code that reads package.json
and returns an array of installed packages:
uninstallCommand
as one of the commands returned by the root command:


Building “Scripts”
“Scripts” is very similar to uninstall, except that we need to surface the output from the script. For this, we use an experimental API called execInShell. Other than that, we can reuse most of the code from “Uninstall”
Building “Install”
“Install” is somewhat different: we are pulling external data from the npm registry in response to the user typing in a search query. And we only want to explicitly trigger this search when the user has indicated that they want to search for npm packages to installsearch
and active
parameters?
active
istrue
when users have selected the “Install” command (as opposed to the command system merely querying for subcommands in advance). We can check for it to make sure that we only query npm when we know that a user is interested in installing an extension.search
returns what the user has typed into the command bar, which we use for searching the npm registry


Exercises left to the reader
We built a basic version of the JavaScript commands extension. This could be improved quite a bit:-
Did you notice that we only use
npm
in all the examples? JavaScript ecosystem has a plethora of package managers, including yarn, pnpm, and bun. How can we support all of them? And can we do it “magically” where someone using this extension doesn’t have to manually select their package manager in our command? (Hint: it involves the lockfiles) - We can probably cache the npm registry fetch call, so when you backspace through any letters, the results for that search query appear instantly.
- We can debounce npm search requests to prevent hitting npmjs.com excessively while you’re typing out the package you’re looking for.
-
What happens if someone uses this command in a replit app that isn’t a JavaScript project? We can probably check for the presence of
package.json
before showing the command. And maybe, if someone doesn’t have apackage.json
yet, we can instead show a command tonpm init
their project!
- Here’s the link to the extension
- Here’s a link to the extension’s source replit app