Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 302
Vinod Sai Avatar asked May 15 '20 13:05

Vinod Sai


People also ask

Can I use npm modules in Deno?

Even though Deno does not support NPM modules directly, it provides a Node Compatibility Library, which allows you to use some NPM modules where necessary.

Can you use node modules in Deno?

Node. js has a plugin system that is incompatible with Deno, and Deno will never support Node.

How do I use npm on Meteor js?

To use an npm package from a file in your application you import the name of the package: import moment from 'moment'; // this is equivalent to the standard node require: const moment = require('moment'); This imports the default export from the package into the symbol moment .

Does Deno have a package manager?

In Deno there is no concept of a package manager as external modules are imported directly into local modules.

How can I use npm packages with Deno?

Using CDNs to access the vast majority of npm packages in ways that work under Deno. How import maps can be used to provide "bare specifier" imports like Node.js under Deno, without needing to use a package manager to install packages locally.

Why can't I use Node JS in Deno?

If the Node.js code you want to use requires a "native" Node.js plugin, it won't work under Deno. Node.js has some built in modules (e.g. like vm) that are effectively incompatible with the scope of Deno and therefore there aren't easy ways to provide a polyfill of the functionality in Deno.

What is dendeno's node compatibility?

Deno has a node compatibility module, which helps us to use npm packages that do not use non-polyfilled node.js APIs.

What is the best ESM to use with Deno?

Pika hosts web-focused ESM packages in their CDN which is fantastic for us to use with deno as this means we can import npm packages that we already know and use in our deno runtime from Pika as an ESM. This is incredible.


4 Answers

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);
like image 50
Marcos Casagrande Avatar answered Oct 19 '22 20:10

Marcos Casagrande


Issue

In general, there are two issues with npm packages in Deno:

  1. ES Module (ESM) conformity is not given.
  • Bare imports like import _ from "lodash" don't work - no "magic" node_modules resolution
  • All import specifiers need to include the file extension - .ts,.js etc.
  • CommonJS module system is not usable in Deno
  1. The npm package uses native Node.js builtins like fs or path.

Solutions to issue 1

1.1: Third party modules

The Third Party Modules section is the quickest way to discover compatible packages.

1.2: ESM CDN providers

Also take a look at CDN providers, that can auto-convert npm packages to ES Modules (ESM):

  • Skypack CDN
  • jspm.io
  • unpkg.com with ?module query parameter

Skypack CDN can deliver auto-converted packages, that e.g. have set a "module" entrypoint in package.json. For TypeScript users: It fetches .d.ts type definitions along with .js files (via X-TypeScript-Types HTTP headers used by Deno).

unpkg.com describes its ?module flag as follows: "Expands all 'bare' import specifiers in JavaScript modules to unpkg URLs. This feature is very experimental".

Esprima does not depend on Node.js builtins, so we can simplify its import by a CDN URL:
import esprima from "https://cdn.skypack.dev/esprima@^4.0.1"; // Option 1: Skypack
import esprima from "https://dev.jspm.io/esprima"; // Option 2: jspm 
// your program
const tokens = esprima.tokenize("const foo = 'bar'"); // works

jspm would be a good choice here - Skypack TS types didn't work for me in this particular case.

1.3: Other approaches

You might also try to import an ESM compatible version directly from repository sources (e.g. an ESM branch). Though for Esprima it won't work because of missing file extensions in code.

Snowpack and jspm stand in for a more manual approach to convert CommonJS → ESM. The rollup plugin @rollup/plugin-commonjs (internally used by Snowpack) is even a more low-level tool.


Solution to issue 2

Deno provides a Node compatibility layer, see Marcos Casagrande's answer. However, not all native Node.js built-ins are fully supported.

As Esprima doesn't rely on Node builtins, you can go with the simpler CDN option.

like image 39
ford04 Avatar answered Oct 19 '22 21:10

ford04


As of version Deno 1.2.5 (released today) deno is now included with experimental npm support.

// main.ts
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/");

You can now run deno run --unstable --A main.ts and express will be downloaded.

like image 2
ThomasReggi Avatar answered Oct 19 '22 20:10

ThomasReggi


Starting with v1.15 Deno provides Node compatibility mode that makes it possible to run a subset of programs authored for Node.js directly in Deno. Compatibility mode can be activated by passing --compat flag in CLI.

deno run  --compat --unstable --allow-read test.js

Currently, not all node.js built-in modules are supported and many are partially supported.

The following modules are not yet implemented:

  • cluster, dgram, http2, https, repl, tls, vm, lib
like image 1
Fuubi Avatar answered Oct 19 '22 20:10

Fuubi