Search code examples
javascriptnode.jsnpmesprimadeno

How to use npm module in DENO?


Deno is super cool. I saw it in the morning and want to migrate to deno now. I was trying to move my existing nodejs script to deno. Can any one help me on how to use npm modules in deno. I need esprima module. This one has the package https://github.com/denoland/deno_third_party/tree/master/node_modules but i am not able to figure out how to use that.


Solution

  • Deno provides a Node Compatibility Library, that allows using some NPM packages that do not use non-polyfilled Node.js APIs.

    As of Deno 1.25 there's an experimental NPM support by using npm: specifier

    npm:<package-name>[@<version-requirement>][/<sub-path>]
    
    import express from "npm:express";
    const app = express();
    
    app.get("/", function (req, res) {
      res.send("Hello World");
    });
    
    app.listen(3000);
    console.log("listening on http://localhost:3000/");
    

    The --unstable flag is required.

    When doing this, no npm install is necessary and no node_modules folder is created


    You can also require and installed npm package by using https://deno.land/std/node/module.ts

    The following works on deno >= 1.0.0

    npm install esprima
    
    import { createRequire } from "https://deno.land/std/node/module.ts";
    
    const require = createRequire(import.meta.url);
    const esprima = require("esprima");
    
    const program = 'const answer = 42';
    console.log(esprima.tokenize(program))
    

    The above code will use esprima from node_modules/.

    To run it, you'll need --allow-read && --allow-env flag

    # you can also use --allow-all
    deno run --allow-read --allow-env esprima.js
    

    You can restrict it only to node_modules

    deno run --allow-read=node_modules esprima.js
    

    Which outputs:

    [
     { type: "Keyword", value: "const" },
     { type: "Identifier", value: "answer" },
     { type: "Punctuator", value: "=" },
     { type: "Numeric", value: "42" }
    ]
    

    Note: many APIs used by std/ are still unstable, so you may need to run it with --unstable flag.


    Although since that whole project is written in TypeScript already, and it's not using any dependencies, it will be very easy for them to adapt it to Deno. All they need to do is use .ts extension on their imports. You can also fork the project and do the changes.

    // import { CommentHandler } from './comment-handler';
    import { CommentHandler } from './comment-handler.ts';
    // ...
    

    Once they do, you'll be able to just do:

    // Ideally they would issue a tagged release and you'll use that instead of master
    import esprima from 'https://raw.githubusercontent.com/jquery/esprima/master/src/esprima.ts';
    
    const program = 'const answer = 42';
    console.log(esprima.tokenize(program))
    

    Alternative

    You can also use https://jspm.io/ which will convert NPM modules to ES Modules

    All modules on npm are converted into ES modules handling full CommonJS compatibility including strict mode conversions.

    import esprima from "https://dev.jspm.io/esprima";
    
    const program = 'const answer = 42';
    console.log(esprima.tokenize(program))
    

    For packages that use Node.js modules not supported by jspm it will throw an error:

    Uncaught Error: Node.js fs module is not supported by jspm core. 
    Deno support here is tracking in 
    https://github.com/jspm/jspm-core/issues/4, +1's are appreciated!
    

    To polyfill those Node.js APIs you'll have to include std/node.

    // import so polyfilled Buffer is exposed                                                                                                  
    import "https://deno.land/std/node/module.ts";
    import BJSON from 'https://dev.jspm.io/buffer-json';
    
    const str = BJSON.stringify({ buf: Buffer.from('hello') })
    
    console.log(str);