node.js - 在 Typescript 中正确导入 native Node 模块

标签 node.js typescript import native

我已经使用 node 很长一段时间了(用于我的后端)和带有 ionic 的 typescript(用于前端)。在 Ionic 上,我意识到仅仅因为 TypeScript,我就设法避免了许多陷阱和错误。因此,我决定将所有纯 JS 后端转换为 TypeScript。

我遇到的第一个障碍是如何正确导入 native Node 模块,如 httposchild_process 等.

在大多数模块上,您通常可以执行类似import { some_export } from 'that_module' 的操作。我还可以在 @types/ 存储库中看到 Node 的类型定义。我试过 import { http, os } from 'node' 但我收到投诉

/node_modules/@types/node/index.d.ts is not a module

因此我的问题是我应该如何导入 native Node 模块?

最佳答案

感谢 this simple tutorial 的一些简单阅读,我已经设法解决了这个问题。

据我了解, native 模块是独立模块,它们未在 node 下命名空间。因此,您应该直接从他们那里导入。

简单地这样做:

import * as http from "http";
import * as os from "os";
import * as path from "path";
.
.
.
and so on

关于node.js - 在 Typescript 中正确导入 native Node 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48438495/

相关文章:

javascript - 在 express 中将路由设置为静态文件

javascript - findItemByText 在 typescript 中未定义

javascript - 使用 UserAgent 检测浏览器

visual-studio-2012 - Web Essentials v2.8 LESS导入错误

java - 如何更改 java.sql.Connection con;进口申报和连接con;

javascript - d3 v4 中的二维画笔

javascript - CRC具体如何使用?

json - 处理路由时出错 : index Assertion Failed: You must include an 'id'

javascript - 使用 WebPack + TypeScript 定义导入的外部模块

php - 通过 HTTP 从远程服务器复制图像