安装的@types/node TypeScript版本无法找到模块"child_process"。

10

我发现了适用于node的typescript版本,但它无法找到来自node软件包的"child_process"模块。

我的控制台中确切的错误消息是模块未找到:错误:无法解析'child_process'

当我查看一些博客时,他们建议在您的tsconfig.json文件中添加以下代码。

"typeRoots": [
      "node_modules/@types"
    ]

和我在这里粘贴的东西一样,不太可能有帮助。

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

为什么他们不注入这个节点模块?我在tsconfig.json文件中做错了什么吗?如果你能给一点帮助,我将不胜感激。提前致谢。

1个回答

12
也许这就是路径:
"typeRoots": [
      "../node_modules/@types"
    ]

通常情况下,您的tsconfig文件位于src目录中。

更新:

我的原始回答没有意义,因为您有这个"baseUrl": "src"

相反,我认为这可能是一个Webpack问题。

尝试将以下内容添加到您的webpack.config文件中:

target: 'node'

更新2:

解决方法:将以下内容添加到您的配置文件中:

node: {
      child_process: 'empty'
}

它不起作用。还有其他方法可以调试以确保路径吗? - QuokMoon
我应该在webpack.config或tsconfig.js文件中进行更改吗? - QuokMoon
@QuokMoon 在你的 webpack.config.js 文件中 - SrAxi
更新2应该放在哪个文件中? 当我粘贴tsconfig.json时,出现了一个错误。 - Kisinga
@SrAxi webpack.config文件在哪里?据我所知,我找不到它。谢谢! - lve
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接