有什么方法可以将Node项目转换为Deno吗? [英] Is there any way to convert a Node project to Deno?

查看:223
本文介绍了有什么方法可以将Node项目转换为Deno吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Node.js项目转换为Deno.有任何指南吗?

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

我当前的项目有很多NPM文件,并且已经在TypeScript中.

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

有什么提示吗?

推荐答案

Deno和Node.js API不兼容,您当然可以重用所有javascript/打字稿代码,但需要重构或添加polyfills

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.

为简化迁移,Deno提供了节点兼容性库std/node,仍然需要很多工作.

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

幸运的是,require是已经受支持的polyfill之一

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'))

如果运行该示例:

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

您可以看到 left-pad 已从node_modules/正确加载.因此,对于不依赖Node.js API的NPM软件包,您可以使用std/node轻松地要求它们.

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 builtins

现在,对于严重依赖Node.js API的软件包,您可以做的最好的事情就是使用Deno API重写它们.

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

随着项目的成熟,将会有更简单的方法将Node.js项目转换为Deno.

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

对于在Node.js上可以完美运行的大型项目而言,IMO并不值得迁移它们.迪诺Node.js可以生活在一起,而不是一个或另一个.如果您愿意,可以在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.

这篇关于有什么方法可以将Node项目转换为Deno吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆