Converting a Node project to Deno

jotafeldmann

Jota Feldmann

Posted on May 13, 2020

Converting a Node project to Deno

I was intrigued to test Ryan Dhal's Deno and nothing better than some personal project to make it right.

Deno

Some important stuff before:

Remove all NPM files and node_modules

You don't need anything more than Deno, but some tasks will be converted to some Deno "out of the box" command (e.g. deno test), and for others, I'll use a Makefile for convenience.

Add file extensions to imports

Add .ts to all import statements.

One easy way using VS Code "search and replace":

  • Enable regex
  • For Search field use from (.+?)(?=.ts')
  • For Replace field use from $1.ts Alt Text

Fix parser warnings and adapt the logic

Deno uses strict guidelines using the TypeScript and style guide. It includes some logical/code adaptations.

Optional: convert tests and test task

# Optional Makefile for convenience
test:
    deno test
Enter fullscreen mode Exit fullscreen mode

Convert install task and add the first dependency

Forget npm install. You can use dep.ts, but it's not required. I'm using a Makefile to keep track of all dependencies:

# Optional Makefile for convenience
install:
    deno install --unstable --allow-read --allow-run -f https://deno.land/x/denon/denon.ts;
Enter fullscreen mode Exit fullscreen mode

Convert run and dev tasks (with Denon)

Here I'm using Denon module, the Nodemon for Deno, to watch and reload file changes.

# Optional Makefile for convenience
dev:
    denon $(ENTRY_POINT)
run:
    deno run $(ENTRY_POINT)
Enter fullscreen mode Exit fullscreen mode

Set entry point

Change the entry point file name from index.ts to mod.ts Deno/Rust standard.

Use my project as a template

All these steps are documented on my project: https://github.com/jotafeldmann/elevators/pull/1

Enjoy and, please, send me feedback to improve.

💖 💪 🙅 🚩
jotafeldmann
Jota Feldmann

Posted on May 13, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related