gpt4 book ai didi

typescript - 在 Typescript 中使用 npm 模块

转载 作者:搜寻专家 更新时间:2023-10-30 20:35:31 26 4
gpt4 key购买 nike

我想在 Typescript 项目中使用 npm 模块,但是这个模块没有 typings 或 tsd。当我尝试使用 import Module from 'module' 时出现错误:Cannot find module 'module'. 有办法修复吗?

[编辑]我的 tsconfig.json:

{
"compilerOptions": {
"target": "ES5",
"moduleResolution": "node",
"module": "commonjs",
"noEmitOnError": true,
"noImplicitAny": true,
"experimentalDecorators": true,
"sourceMap": true,
"sourceRoot": "src",
"outDir": "bld"
},
"exclude": [
"bld"
]
}

最佳答案

我假设你的问题与导入模块有关

import Module from 'module'

并没有像您所说的那样导出它。如果是这种情况,您可以退回到普通的 javascript 并像这样需要模块:

var Module = require('module');

[编辑]

验证在 tsconfig.json 中您在编译器选项中有以下几行:

"compilerOptions": { 
"moduleResolution": "node",
"module": "commonjs"
}

希望这对您有所帮助。

关于typescript - 在 Typescript 中使用 npm 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36309361/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com