Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to convert a Node project to Deno?

I want to convert a Node.js project to Deno. Is there any guide available?

My current project has lots of NPM files and it's already in TypeScript.

Any tips?

like image 818
jotafeldmann Avatar asked May 13 '20 08:05

jotafeldmann


People also ask

Can Deno run node packages?

Node. js has a plugin system that is incompatible with Deno, and Deno will never support Node.

Is Deno better than node?

Node. js uses a package manager like npm or yarn to download third-party packages from the npm registry into a node_modules directory and a package. json file to keep track of a project's dependencies. Deno does away with those mechanisms in favor of a more browser-centric way of using third-party packages: URLs.

Should I learn node js before Deno?

An experienced Node. js developer will be able to pick up Deno and start running with it almost straight away. Deno is worth playing around with if you're already an experienced Node. js developer because you might learn some new concepts and have a small advantage when Deno starts to become popular.

Is it worth it to migrate from Node JS to Deno?

As the project matures there will be easier ways to convert a Node.js project to Deno. IMO for big projects working perfectly on Node.js it's not worth it to migrate them. Deno & Node.js can live together it's not one or the other. Build new projects on Deno if you prefer instead of migrating old ones. Share Improve this answer

How to run node programs in Deno?

Starting with v1.15 Deno provides Node compatiblity mode that makes it possible to run a subset of programs authored for Node.js directly in Deno. Compatiblity mode can be activated by passing --compat flag in CLI.

Can we use Denon as a test runner for Node JS?

We can use Denon as we use deno run to execute scripts. Jest, Jasmine, Ava... In the Node.js ecosystem there are a lot of alternatives for test runners. However, there isn't one official way to test the Node.js code. In Deno, there is an official way, you can use the testing std library. Webpack, Parcel, Rollup...

What is node compatiblity mode in Deno?

Node.js compability mode Starting with v1.15 Deno provides Node compatiblity mode that makes it possible to run a subset of programs authored for Node.js directly in Deno. Compatiblity mode can be activated by passing --compat flag in CLI.


Video Answer


2 Answers

Check out Denoify, it's a build tool that does what you want. You don't have to write the port the tool do it for you, it is all detailed in the docs.

Disclosure: I am the author.

repo

like image 89
Joseph Garrone Avatar answered Oct 16 '22 17:10

Joseph Garrone


Deno and Node.js APIs are not compatible, of course you will be able to reuse all javascript/typescript code but you'll need to refactor or add polyfills.

To ease migration Deno provides a Node Compatibility library, std/node, which still needs a lot of work.

Fortunately require is one of the already supported polyfills

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

const require = createRequire(import.meta.url);
// Loads native module polyfill.
const path = require("path");
// Visits node_modules.
const leftPad = require("left-pad");

console.log(leftPad('5', 5, '0'))

If you run that example:

npm i left-pad
deno run --allow-read node.js
// 00005

As you can see left-pad was loaded correctly from node_modules/. So for NPM packages that do not rely on Node.js API, you can easily require them using std/node.

Here's a list of all supported modules


Right now, for the packages that rely heavily on Node.js API, the best thing you can do is rewrite them using Deno API.

As the project matures there will be easier ways to convert a Node.js project to Deno.

IMO for big projects working perfectly on Node.js it's not worth it to migrate them. Deno & Node.js can live together it's not one or the other. Build new projects on Deno if you prefer instead of migrating old ones.

like image 34
Marcos Casagrande Avatar answered Oct 16 '22 18:10

Marcos Casagrande