升级到Angular 8后进行ng测试“无法解析'fs'”

时间:2019-09-23 21:30:22

标签: angular angular-cli

将Angular项目升级到8后,运行ng-test --source-map false时收到此错误。

注意:我在项目代码中找不到任何地方的fs 导入。 AFAIK,我的项目不需要访问文件系统。

ERROR in ./node_modules/resolve/lib/async.js
Module not found: Error: Can't resolve 'fs' in 'C:\src\myproject\node_modules\resolve\lib'
resolve 'fs' in 'C:\src\myproject\node_modules\resolve\lib'
  Parsed request is a module
  using description file: C:\src\myproject\node_modules\resolve\package.json (relative path: ./lib)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      looking for modules in C:/src/myproject/src/
        using description file: C:\src\myproject\package.json (relative path: ./src/)
          Field 'browser' doesn't contain a valid alias configuration
          using description file: C:\src\myproject\package.json (relative path: ./src/fs)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              C:\src\myproject\src\fs doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              C:\src\myproject\src\fs.ts doesn't exist

似乎有一个名为resolve的库,它需要fs。这是我关于resolve的发现:

yarn why resolve
yarn why v1.16.0
[1/4] Why do we have the module "resolve"...?
[2/4] Initialising dependency graph...
[3/4] Finding dependency...
[4/4] Calculating file sizes...
=> Found "resolve@1.12.0"
info Has been hoisted to "resolve"
info Reasons this module exists
   - Hoisted from "tslint#resolve"
   - Hoisted from "grunt-cli#liftoff#resolve"
   - Hoisted from "@angular-devkit#build-angular#postcss-import#resolve"
   - Hoisted from "@angular-devkit#build-angular#@babel#core#resolve"
   - Hoisted from "grunt-cli#liftoff#rechoir#resolve"
   - Hoisted from "@angular#cli#pacote#normalize-package-data#resolve"
=> Found "grunt#resolve@1.1.7"
info Reasons this module exists
   - "grunt#grunt-cli" depends on it
   - Hoisted from "grunt#grunt-cli#resolve"
Done in 0.71s.

像角度的CLI生成和tslint之类的命令行工具应该访问文件系统似乎很正常...那为什么我的项目失败了?

yarn why下列出了devDependencies中提到的所有上述库(tslint,grunt-cli,angular-devkit,angular / cli),这对我来说似乎可以。

更新

在解决了一些其他问题之后,我现在在ng serveng test上都遇到此错误。这是我的tsconfig文件:

tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types",
      "src/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

tsconfig.spec.json:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/spec",
    "types": [
      "jasmine",
      "node"
    ]
  },
  "files": [
    "src/test.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.spec.ts",
    "src/**/*.d.ts"
  ]
}

tsconfig.app.json:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": [
      "node"
    ]
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "src/test.ts",
    "**/*.spec.ts"
  ]
}

1 个答案:

答案 0 :(得分:0)

经过漫长的反复试验,我找到了罪魁祸首:

import {hasOwnProperty} from 'tslint/lib/utils';

我不小心将tslint实用程序函数导入了我的应用程序源代码。糟糕!