【问题标题】:tsc not excluding node_modulestsc 不排除 node_modules
【发布时间】:2016-09-15 12:35:09
【问题描述】:
{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

我正在尝试将 angular2/beta8 应用升级到 RC1,并且我正在根据快速入门指南进行基本重组。

我将它的 tsconfig.json 复制到我的项目目录中。我想我已经准备好了所有其他东西,但是当我运行tsc 时,我的 node_modules 文件夹中的文件中出现了各种错误。为什么它甚至一开始就在那里寻找?

【问题讨论】:

标签: typescript angular tsc


【解决方案1】:

tsconfig.json

中添加此选项
{
  "compilerOptions": {
    "skipLibCheck": true
  },
}

【讨论】:

    【解决方案2】:

    我不知道你是否找到了这个 Alex 的答案,但是 LDL 在 cmets 中提到的 question/answer 提供了一个答案,由 Srikanth Injarapu 提交。

    以下是答案,以防万一有人不想访问该链接:

    如果您的目标是 ES5,请添加 "node_modules/typescript/lib/lib.es6.d.ts" 到 tsconfig.json 文件:

     {    
       "compilerOptions": {
         "module": "commonjs",
         "target": "es5",
         "noImplicitAny": false,
         "outDir": "built",
         "rootDir": ".",
         "sourceMap": false
       },
       "files": [
         "helloworld.ts",
         "node_modules/typescript/lib/lib.es6.d.ts"
       ],
       "exclude": [
         "node_modules"
       ]
     }
    

    编辑

    在我的应用程序中,我使用 webpack 来构建我的应用程序,但在控制台上仍然出现相同的错误。我目前正在寻找解决此问题的方法,并将报告我的发现。

    【讨论】:

    • 可以肯定的是,在 2019 年,解决方案是将 "lib": ["es6"], 添加到 compilerOptions
    • 不,添加 "lib": ["es6"], 对我不起作用。不过,原来的答案确实如此。
    • 对于 Webpack 4.43.0,您只需要:javascript "files": [ "src/index.ts", "node_modules/typescript/lib/lib.es6.d.ts" ] 其中 src/index.ts 是我的条目之一。
    【解决方案3】:

    对于使用 webpack 的任何人,添加“排除”属性对我不起作用。

    而是将所有文件扩展名添加到 webpack 的“resolve”属性并从任何有效的“规则”对象中排除 node_modules:

    resolve: {
        extensions: ['*', '.ts', '.tsx', '.js']
    },
    module: {
         rules: [
                {
                   test: /\.ts(x?)$/,
                   exclude: /node_modules/,
                   use: [
                        {
                            loader: "ts-loader"
                        }
                   ]
                 },
                 // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
                 {
                    enforce: "pre",
                    test: /\.js$/,
                    exclude: /node_modules/,
                    loader: "source-map-loader"
                 },
               ]
    

    【讨论】:

      猜你喜欢
      • 2017-03-23
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      • 2019-11-11
      • 1970-01-01
      • 2019-11-16
      相关资源
      最近更新 更多