search
HomeWeb Front-endJS TutorialHow to use Node module in Deno?

How to use Node module in Deno?

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

Deno is 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 the Deno standard library or Deno 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 the import syntax. 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 the import syntax of standards-compliant ES modules.

Some software packages have separate src/ and dist/ directories, where ES module-style code is located in src/, 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 use github cdn or 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 popular web -push library. 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 named fast-xml-parser, I noticed that it had a type definition file ending with .d.ts of. These files describe various interfaces and even apply to JavaScript.js files. Sometimes you can also find type definition files in the @types\somemodule repository.

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 as FastXMLParser (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 as fastXMLParser (lowercase f), which is valid code, but has no type.

Next, combine them together to create a parser, a fastXMLParser of type FastXMLParser.

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. If there is any infringement, please contact admin@php.cn delete
Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor