Photographer: Jason Zi Feng Lei

EDIT: I’ve now made a module for ease of use, you can find it here.

How to use Lodash with Deno

Recently I have had the pleasure of playing with Ryan Dahl’s newest creation — Deno. So far it has been really great! However being a relative new project that’s under active development, it does have its hiccups here and there. One of those hiccups I encountered was using Lodash.

I’ve been using lodash for around 3 years now and it’s a utility library that I just can’t seem to shake even with new features of ES6/7 being available. Personally I just feel that it reads cleanly and flows nicely (chained functions). So naturally, when I started tinkering with Deno, I had to import Lodash.

Oh boy was I happy when I found out that it was listed in the third party modules list on Deno’s official website! That happiness quickly turned into frustration when I couldn’t for the life of me find any documentation on importing it. Well after a long amount of frustration, digging, and asking I figured it out.

Before we begin, let’s just lay out or project directory:

We need to get the Lodash library exported as NPM modules. Simple enough, go ahead and either clone or download the repository from here. As you can see we’re checking out the npm branch. Once you’re done, you should have a directory called lodash-npm in your project directory. Next let’s make a new Typescript file in our project directory; lodash.ts . The last part to making this all work is these magic lines:

import { createRequire } from 'https://deno.land/std/node/module.ts'; const Lodash = createRequire(import.meta.url)('./lodash'); export default Lodash;

Basically, thanks to Kevin Qian we can now require and use CommonJS modules such as Lodash — you can check out his tweet about it.

4. So to test it, you can create a new test file mod.ts and put the following code in:

import Lodash from './lodash.ts'; console.log(Lodash);

You should see the following in the terminal when you run deno allow-read mod.ts :

[Function: lodash]

If you do, then lodash has been successfully loaded and working! I hope this helps.