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 mo
In general, there are two issues with npm packages in Deno:
import _ from "lodash" don't work - no "magic" node_modules resolution.ts,.js etc.fs or path.The Third Party Modules section is the quickest way to discover compatible packages.
Also take a look at CDN providers, that can auto-convert npm packages to ES Modules (ESM):
?module query parameterPika 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".
import esprima from "https://cdn.pika.dev/esprima@^4.0.1"; // Option 1: Pika
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 - Pika TS types didn't work for me in this particular case.
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.
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.