德诺超级酷.我早上看到的,现在想迁移到德诺.我的脚本试图移动.有谁能帮助我在deno中使用npm模块吗.我需要一个模块.这个有https://github.com/denoland/deno_third_party/tree/master/node_modules个包裹,但我不知道如何使用.

推荐答案

Deno提供了一个Node Compatibility Library,这将允许使用一些不使用non-polyfilled Node.js APIs的NPM包.你可以用https://deno.land/std/node/module.ts下载这个软件包

以下是关于deno 1.0.0的作品

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

const require = createRequire(import.meta.url);
const esprima = require("esprima");

const program = 'const answer = 42';
console.log(esprima.tokenize(program))

以上代码将使用node_modules/中的esprima.

要运行它,你需要--allow-read个标志

deno run --allow-read esprima.js

您只能将其限制为node_modules

deno run --allow-read=node_modules esprima.js

哪些输出:

[
 { type: "Keyword", value: "const" },
 { type: "Identifier", value: "answer" },
 { type: "Punctuator", value: "=" },
 { type: "Numeric", value: "42" }
]

Note:std/使用的许多API仍然是unstable,所以您可能需要使用--unstable标志运行它.


虽然整个项目都是用TypeScript编写的,而且没有使用任何依赖项,但他们很容易将其应用到Deno中.他们所需要做的就是在their imports上使用.ts扩展.

// import { CommentHandler } from './comment-handler';
import { CommentHandler } from './comment-handler.ts';
// ...

一旦他们这么做了,你就可以:

// Ideally they would issue a tagged release and you'll use that instead of master
import esprima from 'https://raw.githubusercontent.com/jquery/esprima/master/src/esprima.ts';

const program = 'const answer = 42';
console.log(esprima.tokenize(program))

可供替代的

您还可以使用https://jspm.io/将NPM模块转换为ES模块

npm上的所有模块都转换为ES模块,以处理完整的数据

import esprima from "https://dev.jspm.io/esprima";

const program = 'const answer = 42';
console.log(esprima.tokenize(program))

对于使用Node的包.jspm不支持js模块它将抛出一个错误:

Uncaught Error: Node.js fs module is not supported by jspm core. 
Deno support here is tracking in 
https://github.com/jspm/jspm-core/issues/4, +1's are appreciated!

目前,您可以使用仅使用Buffer的软件包,因此必须包含std/node.

// import so polyfilled Buffer is exposed                                                                                                  
import "https://deno.land/std/node/module.ts";
import BJSON from 'https://dev.jspm.io/buffer-json';

const str = BJSON.stringify({ buf: Buffer.from('hello') })

console.log(str);

Node.js相关问答推荐

Spotify Auth访问令牌给出错误代码400

Mongoose-如何从父文档填充到子文档

模块';"; node :流程&没有导出的成员';dlopen';

如何使用mongoose引用不在项目中的模型

MongoDB - mongoose :如何查询这个? 填充()不起作用.它显示空

TypeError:在使用 Jest、Supertest、Express、Typescript 进行测试时无法读取未定义的属性(读取listen)

将 express js app.use() 移动到另一个文件

使用 grunt 服务器,如何将所有请求重定向到根 url?

提供静态文件到底是什么意思?

使用 WebSockets 有服务器成本吗?

如何使用 gulp-uglify 缩小 ES6 函数?

使用 NodeJS 将新对象附加到 DynamoDB 中的 JSON 数组

为什么我们要为 Angular 2.0 安装 Node.js?

tsconfig.json 中模块类型的区别

Bootstrap 中的 Grunt 依赖冲突

mongo 可以 upsert 数组数据吗?

当进程被杀死时,如何优雅地关闭我的 Express 服务器?

从 React(同构应用程序)进行 API 调用时出现Access-Control-Allow-Origin问题

卸载代码/模块

AWS Lambda 函数写入 S3