[英]Is there any way to convert a Node project to Deno?
I want to convert a Node.js project to Deno.我想将 Node.js 项目转换为 Deno。 Is there any guide available?
有没有可用的指南?
My current project has lots of NPM files and it's already in TypeScript.我当前的项目有很多 NPM 文件,并且已经在 TypeScript 中。
Any tips?有小费吗?
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 和 Node.js API 不兼容,当然您可以重用所有 javascript/typescript 代码,但您需要重构或添加 polyfill。
To ease migration Deno provides a Node Compatibility library , std/node
, which still needs a lot of work.为了简化迁移,Deno 提供了一个节点兼容性库
std/node
,它仍然需要做很多工作。
Fortunately require
is one of the already supported polyfills幸运的是
require
是已经支持的 polyfill 之一
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/
.如您所见
left-pad
已从node_modules/
正确加载。 So for NPM packages that do not rely on Node.js API, you can easily require them using std/node
.因此,对于不依赖于 Node.js API 的 NPM 包,您可以使用
std/node
轻松要求它们。
Here's a list of all supported builtins这是所有受支持的内置函数的列表
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 API 的软件包,您可以做的最好的事情是使用 Deno API 重写它们。
As the project matures there will be easier ways to convert a Node.js project to Deno.随着项目的成熟,将有更简单的方法将 Node.js 项目转换为 Deno。
IMO for big projects working perfectly on Node.js it's not worth it to migrate them.对于在 Node.js 上完美运行的大型项目,IMO 不值得迁移它们。 Deno & Node.js can live together it's not one or the other.
Deno 和 Node.js 可以住在一起,不是其中之一。 Build new projects on Deno if you prefer instead of migrating old ones.
如果您愿意,可以在 Deno 上构建新项目,而不是迁移旧项目。
Starting from Deno v1.15 there is Node.js compatibility mode which is unstable and comes with some caveats.从 Deno v1.15 开始,有Node.js 兼容模式,该模式不稳定并带有一些警告。 There is also an issue in the repo tracking the progress of the Node Compat mode.
在跟踪 Node Compat 模式的进度的 repo 中也存在问题。
Node.js compability mode
Node.js 兼容模式
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.
从 v1.15 开始,Deno 提供了 Node 兼容模式,可以直接在 Deno 中运行为 Node.js 编写的程序子集。 Compatiblity mode can be activated by passing --compat flag in CLI.
可以通过在 CLI 中传递 --compat 标志来激活兼容模式。
⚠️ Using compatiblity mode currently requires the --unstable flag.
⚠️ 目前使用兼容模式需要 --unstable 标志。 If you intend to use CJS modules, the --allow-read flag is needed as well.
如果您打算使用 CJS 模块,还需要 --allow-read 标志。
⚠️ Package management is currently out of scope for Node.js compatiblity mode.
⚠️ Package 管理当前不在 Node.js 兼容模式下的 scope。 For the time being we suggest to keep using your current solution (npm, yarn, pnpm).
暂时我们建议继续使用您当前的解决方案(npm、yarn、pnpm)。
Please checkout the docs for请检查文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.