Converting a Node project to Deno
Jota Feldmann
Posted on May 13, 2020
I was intrigued to test Ryan Dhal's Deno and nothing better than some personal project to make it right.
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.
- Remove
package.json
,package-lock.json
, and all related stuff. Check this file for allNPM
possibilities: https://github.com/github/gitignore/blob/master/Node.gitignore - Remove
node_modules
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
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
- Short version: https://github.com/denoland/deno/blob/master/docs/testing.md
- Follow the "out of the box" test suite https://deno.land/std/testing
- Convert from
npm test
to something like:
# Optional Makefile for convenience
test:
deno test
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;
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)
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.
Posted on May 13, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.