Skip to main content
The Deno 2 Release Candidate is here
Learn more

npm_bridge

This is a proof of concept I threw together in a few hours. DO NOT USE THIS AS IT LIKELY WON’T WORK. I haven’t implemented a lot.

Proof of concept for a bridge between Deno and npm packages.

Works similarly to using npm in Node.

Step 1: Setup

In the root of your application:

  1. Create a deno.json file and add a “npm” task:
    {
      "tasks": {
        "npm": "deno run -A --no-check --reload https://deno.land/x/npm_bridge/main.ts"
      }
    }

Now run these commands similarly to as you would with npm:

  1. Run deno task npm init to initialize a npm_deps.json file.
  2. Add dependencies (ex. deno task npm install ts-morph, deno task npm i --save-dev mkdirp)

Step 2: Use dependencies

  1. Import and use the package via a bare specifier:
    // main.ts
    import { Project } from "ts-morph";
    
    const project = new Project();
    const sourceFile = project.createSourceFile("test.ts", "class MyClass {}");
    console.log(sourceFile.getClassOrThrow("MyClass").getName());
  2. Run it:
    deno run --allow-read --allow-env --unstable main.ts

Use Binary Dependencies

To use a binary dependency, create a new entry in your deno.json that references the ./npm_deps/<package-name>.bin.js file:

{
  "tasks": {
    // ...
    "mkdirp": "deno run -A --unstable ./npm_deps/mkdirp.bin.js"
  }
  // ...
}

Now try it out:

deno task mkdirp subdir/newdir

What this does

  1. Runs npm install with the dependencies you specified.
  2. Analyzes the node_modules folder and creates wrapper ESM modules around the CJS code.
  3. Creates an import map to map bare specifiers to the wrapper ESM modules and to map import specifiers with no extensions to extensions.
  4. Updates or adds the import map to your deno.json file.

Subcommands

deno task npm init

Initializes a npm_deps.json file.

deno task npm install

Installs the packages specified in npm_deps.json into a npm_deps folder.

deno task npm install <package-name>

Adds a package to the npm_deps.json dependencies and installs it.

deno task npm install --save-dev <package-name>

Adds a package to the npm_deps.json devDependencies and installs it.