如何在DENO中使用npm模块? [英] How to use npm module in DENO?

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

问题描述

Deno非常酷.我早上看过它,现在想迁移到deno.我试图将现有的nodejs脚本移至deno.谁能帮助我使用deno中的npm模块.我需要esprima模块.该软件包具有 https://github.com/denoland/deno_third_party/tree/master/node_modules ,但是我不知道如何使用它.

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提供了

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

以下内容适用于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.

The above code will use esprima from node_modules/.

要运行它,您需要--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" }
]

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

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

尽管整个项目已经用TypeScript编写,并且没有使用任何依赖关系,但他们很容易将其适应Deno.他们所需要做的就是在其导入文件上使用.ts扩展名. 您还可以派生项目并进行更改.

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

替代

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

Alternative

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

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

All modules on npm are converted into ES modules handling full CommonJS compatibility including strict mode conversions.

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

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

对于使用jspm不支持的Node.js模块的软件包,它将引发错误:

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!

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

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模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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