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