更新到 Angular 8 后测试 "Cannot resolve 'fs'"
ng test "Cannot resolve 'fs'" after updating to Angular 8
将我的 Angular 项目升级到 8 后,我在 运行 ng-test --source-map false
时收到此错误。
注意: 我无法在我的项目代码中找到 fs
anywhere 的导入。 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.
angular CLI build 和 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"
]
}
经过长时间的摸索排除,找到了罪魁祸首:
import {hasOwnProperty} from 'tslint/lib/utils';
我不小心将 tslint
实用程序函数导入了我的应用程序源代码。糟糕!
将我的 Angular 项目升级到 8 后,我在 运行 ng-test --source-map false
时收到此错误。
注意: 我无法在我的项目代码中找到 fs
anywhere 的导入。 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.
angular CLI build 和 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"
]
}
经过长时间的摸索排除,找到了罪魁祸首:
import {hasOwnProperty} from 'tslint/lib/utils';
我不小心将 tslint
实用程序函数导入了我的应用程序源代码。糟糕!