Coder Social home page Coder Social logo

npm_bridge's Introduction

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.

npm_bridge's People

Contributors

dsherret avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.