【问题标题】:Typescript error "Cannot find name 'require'. and Cannot find name 'process'."打字稿错误“找不到名称'require'。并且找不到名称'process'。”
【发布时间】:2018-07-23 06:41:42
【问题描述】:

Typescript 错误“找不到名称 'require'。并且找不到名称 'process'。”即使它已经在其编译器选项中定义了参数 "moduleResolution": "node"

以下是我当前的tsconfig.json

{
  "compilerOptions": {
     "target": "es2015",
     "lib": ["es2015", "es6"],
     "types": ["reflect-metadata"],
     "moduleResolution": "node",
     "module": "commonjs",
     "rootDir": "src",
     "outDir": "./build",
     "emitDecoratorMetadata": true,
     "experimentalDecorators": true,
     "sourceMap": true,
     "declaration": false
  },
  "include": [
     "src/**/*"
  ],
  "exclude": [
     "node_modules"
  ]
}

【问题讨论】:

    标签: typescript compiler-errors


    【解决方案1】:

    对我来说,这是通过将 types 添加到 angular 8 中的 angular 编译器选项来解决的。

    "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true,
    "types": [ "node" ]
    }
    

    【讨论】:

      【解决方案2】:

      默认情况下不捆绑 Node.js 标准库的类型定义。您需要通过@types/node 包自己安装它们:

      npm install --save @types/node
      

      moduleResolution 选项控制how the compiler handles import statements,与 Node.js 标准库无关。

      要修复“Cannot find name 'require'”错误,您还应该将任何 CommonJS 导入转换为使用 TypeScript 导入语法:

      // Before
      var process = require('process');
      
      // After
      import process = require('process');
      

      有关详细信息,请参阅 TypeScript 手册的 Migrating from JavaScript 部分。

      【讨论】:

        猜你喜欢
        • 2017-11-09
        • 2017-06-18
        • 1970-01-01
        • 2023-03-17
        • 2017-12-01
        • 1970-01-01
        • 2019-04-30
        • 2018-11-09
        • 1970-01-01
        相关资源
        最近更新 更多