ng test "Не удается разрешить 'fs'" после обновления до Angular 8 - PullRequest
1 голос
/ 24 сентября 2019

После обновления моего 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 (tslint, grunt-cli, angular-devkit, angular / cli) перечислены в devDependencies, что мне кажется правильным.

Обновление

После проработки нескольких других проблем, я получаю эту ошибку сейчас как для ng serve, так и ng 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 голосов
/ 30 сентября 2019

После длительного процесса проб и ошибок я обнаружил виновника:

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

Я случайно импортировал служебную функцию tslint в исходный код своего приложения.Oops!

...