首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将节点标准模块导入TypeScript

将节点标准模块导入TypeScript
EN

Stack Overflow用户
提问于 2021-03-28 23:04:48
回答 1查看 263关注 0票数 1

我认为这是一个非常简单的问题,但由于某种原因,我在网上找不到确切的答案:在TypeScript中导入标准NodeJS库的正确方法是什么?

我已经安装了@types/node,但是当我尝试从它导入时,我只得到了错误。例如,我正在尝试导入net。在JS中:

代码语言:javascript
运行
复制
const net = require('net');

在TS中,我尝试过:

代码语言:javascript
运行
复制
import * as net from 'net';  // Cannot find module 'net' or its corresponding type declarations.ts(2307)
import * as net from 'node/net';  // File '[...]@types/node/net.d.ts' is not a module.ts(2306)
import * as net from 'node:net';  // Cannot find module 'node:net' or its corresponding type declarations.ts(2307)

我的tsconfig.json

代码语言:javascript
运行
复制
{
    "compilerOptions": {
        "target": "ES6",
        "module": "commonjs",
        "baseUrl": "./",
        "experimentalDecorators": true,
        "strictPropertyInitialization": false,
        "isolatedModules": false,
        "strict": false,
        "noImplicitAny": false,
        "typeRoots": [
            "../node_modules/@types"
        ],
        "moduleResolution": "node",
        "outDir": "dist"
    },
    "compileOnSave": true,
    "exclude": [
        "../node_modules"
    ],
    "include": [
        "./**/*.ts"
    ]
}

我敢打赌这完全是新手犯的错误,但我真的很困惑。任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-29 00:23:28

你的tsconfig.json文件中有一个拼写错误,你的node_modules文件夹和你的tsconfig.json文件在同一个目录中,所以typeRoots键应该是"typeRoots": ["node_modules/@types"],或者你可以直接删除它,因为它被认为是可见的(从typescript文档中):

默认情况下,所有可见的“@types”包都包含在编译中。任何封闭文件夹的node_modules/@type中的包都被认为是可见的。

还可以在排除键中修复路径

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66842838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档