栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在DENO中使用npm模块?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在DENO中使用npm模块?

Deno提供了一个节点兼容性库,该库将允许使用一些不使用未填充Node.js
API的
NPM软件包。您将可以

require
通过使用
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))

上面的代码将使用

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" }]

注意 :所使用的许多API

std/
仍然不稳定,因此您可能需要使用
--unstable
flag
运行它。


尽管由于整个项目已经使用Typescript编写,并且没有使用任何依赖关系,但是对于他们来说,将其适应Deno还是很容易的。他们需要做的就是

.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 masterimport esprima from 'https://raw.githubusercontent.com/jquery/esprima/master/src/esprima.ts';const program = 'const answer = 42';console.log(esprima.tokenize(program))


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569030.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号