dsherret / npm_bridge

Proof of concept for a bridge between Deno and npm packages

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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, create a deno.json file and add a npm & npx task:

{
  "tasks": {
    "npm": "deno run -A --no-check https://deno.land/x/npm_bridge@{VERSION}/npm.ts",
    "npx": "deno run -A --no-check https://deno.land/x/npm_bridge@{VERSION}/npx.ts"
  }
}

Step 2: Initialize

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

  1. deno task npm init - Initialize a npm_deps.json file (similar to package.json).
  2. deno task npm install <package-name> - Add a dependency.
    • Alternatively: deno task npm i <package-name>
    • For dev dependency: deno task npm i --save-dev <package-name>

Step 3: Use dependencies

  1. Import and use the package via a bare specifier.

    For example, if we did deno task npm i ts-morph above:

    // 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, run deno task npx <binary-name>.

For example:

deno task npx --allow-write=. mkdirp subdir/newdir

You may wish to alias these in another deno task.

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.

deno task npx <...deno-args> <sub-command> <...sub-command-args>

For running a sub command.

About

Proof of concept for a bridge between Deno and npm packages

License:MIT License


Languages

Language:TypeScript 71.1%Language:JavaScript 24.0%Language:Rust 4.9%