本文介绍了如何在 DENO 中使用 npm 模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

Deno 超级酷.我早上看到了,现在想迁移到 deno.我试图将我现有的 nodejs 脚本移动到 deno.任何人都可以帮助我了解如何在 deno.js 中使用 npm 模块.我需要 esprima 模块.这个有包 https://github.com/denoland/deno_third_party/tree/master/node_modules 但我不知道如何使用它.

解决方案

Deno 提供了一个 Node兼容性库,这将允许使用一些不使用 非 polyfill Node.js API.您将能够使用 https://deno.land/std/node/module.ts

require

以下适用于 deno 1.0.0

import { createRequire } from https://deno.land/std/node/module.ts";const require = createRequire(import.meta.url);const esprima = 要求(esprima");const 程序 = 'const 答案 = 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

输出:

[{ 类型:关键字",值:const";},{ 类型:标识符",值:答案";},{ 类型:标点符号",值:=";},{ 类型:数字",值:42"}]

注意:std/ 使用的许多 API 仍然是 unstable,因此您可能需要使用 --unstable 标志运行它.


尽管整个项目已经是用 TypeScript 编写的,并且没有使用任何依赖项,但他们很容易将其适配到 Deno.他们需要做的就是在 .ts 扩展名>他们的进口.您也可以 fork 项目并进行更改.

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

一旦他们这样做,您就可以:

//理想情况下,他们会发布一个标记版本,你将使用它而不是 master从'https://raw.githubusercontent.com/jquery/esprima/master/src/esprima.ts'导入esprima;const 程序 = 'const 答案 = 42';console.log(esprima.tokenize(program))

替代方案

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

npm 上的所有模块都转换成 ES 模块处理完整CommonJS 兼容性,包括严格模式转换.

从https://dev.jspm.io/esprima"导入esprima;const 程序 = 'const 答案 = 42';console.log(esprima.tokenize(program))

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

未捕获的错误:jspm 核心不支持 Node.js fs 模块.这里的 Deno 支持正在跟踪https://github.com/jspm/jspm-core/issues/4,感谢+1!

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

//导入所以 polyfill Buffer 被暴露导入https://deno.land/std/node/module.ts";从'https://dev.jspm.io/buffer-json'导入BJSON;const str = BJSON.stringify({ buf: Buffer.from('hello') })控制台日志(字符串);

Deno is super cool. I saw it in the morning and want to migrate to deno now. I was trying to move my existing nodejs script to deno. Can any one help me on how to use npm modules in deno. I need esprima module. This one has the package https://github.com/denoland/deno_third_party/tree/master/node_modules but i am not able to figure out how to use that.

解决方案

Deno provides a Node Compatibility Library, that will allow to use some NPM packages that do not use non-polyfilled Node.js APIs. You'll be able to require the package by using https://deno.land/std/node/module.ts

The following works on 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))

The above code will use esprima from node_modules/.

To run it, you'll need --allow-read flag

deno run --allow-read esprima.js

You can restrict it only to node_modules

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

Which outputs:

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

Note: many APIs used by std/ are still unstable, so you may need to run it with --unstable flag.


Although since that whole project is written in TypeScript already, and it's not using any dependencies, it will be very easy for them to adapt it to Deno. All they need to do is use .ts extension on their imports.You can also fork the project and do the changes.

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

Once they do, you'll be able to just do:

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

Alternative

You can also use https://jspm.io/ which will convert NPM modules to ES Modules

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

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

For packages that use Node.js modules not supported by jspm it will throw an error:

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!

For now, you can use packages that only use Buffer, for that you'll have to include 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);

这篇关于如何在 DENO 中使用 npm 模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 12:58