1

I've come across this post, that explains how to use (some)NPM modules in Deno: How to use npm module in DENO?

Problem is, the example libraries they give have 0 dependencies.

But what happens, if i want to use a dependency like Axios(might be a bad example because it might not even work with the Node compatibility layer, but it's just an example), which has its own dependencies?

Is there any way to do this, besides manually copying those libraries to my code?

i.brod
  • 3,993
  • 11
  • 38
  • 74

1 Answers1

1

Problem is, the example libraries they give have 0 dependencies.

It works the same way for packages with multiple dependencies.

import { createRequire } from "https://deno.land/std/node/module.ts";

const require = createRequire(import.meta.url);
const isEven = require("is-even");

console.log(isEven(2))

As long as the dependencies don't use non-polyfilled Node.js APIs it will work just fine.


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 isEven  from 'https://dev.jspm.io/is-even';

console.log(isEven(2))
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
  • I see you've required a library called "is-even" by using the "magical" npm convention. This implies the usage of npm, within Deno. Is this what you meant? That i need to make npm init, and install the library? – i.brod Jul 09 '20 at 17:57
  • I don't know what you mean by `magical` NPM convention, but if you want to use an NPm package, all you have to do is; `npm install is-even` the same way you di in Node. – Marcos Casagrande Jul 09 '20 at 18:16
  • `This implies the usage of npm, within Deno.` that's exactly what your question is about – Marcos Casagrande Jul 09 '20 at 18:23