Home>Article>Web Front-end> How to use Node module in Deno?

How to use Node module in Deno?

青灯夜游
青灯夜游 forward
2020-09-02 10:17:16 2356browse

How to use Node module in Deno?

Although the method is not very good, sometimes there is no choice.

Denois a server-side code execution environment based on web technology.

  • Node uses JavaScript and commonjs modules, and uses npm/yarn as its package manager. [Video tutorial recommendation:node js tutorial]
  • Deno uses Typescript or JavaScript, as well as modern javascript import statements. It doesn't require a package manager.

If you want to import a module in deno, you should refer to it through the URL:

import { serve } from "https://deno.land/std/http/server.ts";

You can find it in theDeno standard libraryorDeno third-party module list You can find more needed modules in, but they don't cover everything you need. Sometimes you can only use modules that rely on the npm ecosystem. Here are some methods from the most convenient to the most cumbersome:

1. If the module uses the import/export syntax of the ES module.

The libraries you use in deno do not have to come from the recommended Deno packages, they can come from any URL, as long as they use theimportsyntax. A good way to access these files directly from within the npm repository is through unpkg.

import throttle from https://unpkg.com/lodash@4.17.19/throttle.js

2. If the module itself does not use imports, but the source code does

If it is a module compiled through npm, or a module with the wrong format is used, then whether to use its source code may Requires some luck. The source code of many popular libraries has been migrated from commonjs to conform to theimportsyntax of standards-compliant ES modules.

Some software packages have separatesrc/anddist/directories, where ES module-style code is located insrc/, but npm It is not included in the package. In this case, you can import directly from the source.

import throttle from "https://raw.githubusercontent.com/lodash/lodash/master/throttle.js";

You can get this URL by clicking the "raw" button on github, and then get the original JS file. It's more straightforward, but doable, to usegithub cdnor see if the file is accessible via the github page.

Special Note:Some libraries use ES modules with webpack, or use a module loader so that they can be imported from Node modules, like this:

//不好的用法: import { someFunction } from "modulename"; import { someOtherFunction } from "modulename/file.js";

The standard import method is to start with./or a URL that can work properly:

//标准的用法: import { someOtherFunction } from "./folder/file.js";

However, you can also try the next method:

3. Import commonjs module

Fortunately, there is a service calledJSPM, which can parse third-party modules and compile commonjs modules to be used as ES module imports. This tool can be used to use Node modules in the browser without a build step. But we can use it here too.

In my recent project, I want to do push notifications, which involves generating credentials for VAPID. There is a deno password library that can be encrypted, but the whole development process is difficult. I would rather use the popularweb -pushlibrary. You can use JSPM CDN and the following URL to import:

import webPush from "https://dev.jspm.io/web-push";

This way you can use it in deno like any other module.

Enable Typescript types to work properly

A nice feature of using typecipt in deno is that it can easily provide a perfect auto-completion function for modules. The editor's deno extension can even autocomplete third-party modules if it knows the type definition.

Although this is not necessary for the code to work properly, it can help you maintain the code well.

When I imported another module namedfast-xml-parser, I noticed that it had a type definition file ending with.d.tsof. These files describe various interfaces and even apply to JavaScript.js files. Sometimes you can also find type definition files in the@types\somemodulerepository.

For example:https://github.com/Definitely...

This file typescript can automatically complete the content imported from the JavaScript file. This is true even for files imported with JSPM:

// 导入 fast-xml-parser 库 import fastXMLParser from "https://dev.jspm.io/fast-xml-parser"; // 从 fast-xml-parser 的源代码导入类型定义文件 import * as FastXMLParser from "https://raw.githubusercontent.com/NaturalIntelligence/fast-xml-parser/master/src/parser.d.ts"; //将 parser 与以下类型一起使用 const parser = fastXMLParser as typeof FastXMLParser;

I imported the type definition from the definition file asFastXMLParser(note the capital F), which does not contain any valid code, But this is an object of the same type as the code we're importing.

I imported the code from JSPM asfastXMLParser(lowercase f), which is valid code, but has no type.

Next, combine them together to create aparser, afastXMLParserof typeFastXMLParser.

Finally I hope you can give it a trydeno. Deno's ability to use any module used for the Web or even for node/npm has indeed laid a good foundation for this new server-side library ecosystem.

For more programming-related knowledge, please visit:Programming Teaching! !

The above is the detailed content of How to use Node module in Deno?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete