【发布时间】:2019-10-19 15:57:31
【问题描述】:
当我尝试使用 writeFile angular 从 fs 导入它时,我尝试运行 ng serve 并得到 ERROR in src/app/app.component.ts(2,27): error TS2307: Cannot find module 'fs'.
我猜我错过了一个简单的选项,比如当你想使用 ng-model 时,你需要在 app.modules.ts 中导入 formsModule。
我正在使用:
vscode: 1.34
Angular CLI: 8.0.1
Node: 10.15.3
OS: win32 x64
Angular: 8.0.0
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.800.1
@angular-devkit/build-angular 0.800.1
@angular-devkit/build-optimizer 0.800.1
@angular-devkit/build-webpack 0.800.1
@angular-devkit/core 8.0.1
@angular-devkit/schematics 8.0.1
@angular/cli 8.0.1
@ngtools/webpack 8.0.1
@schematics/angular 8.0.1
@schematics/update 0.800.1
rxjs 6.4.0
typescript 3.4.5
webpack 4.30.0
我的tsconfig 看起来像这样:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "esnext",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es2015",
"typeRoots": ["node_modules/@types"],
"lib": ["es2018", "dom"],
"types": ["node"]
}
}
【问题讨论】:
-
fs是一个 NodeJS 库。它需要访问操作系统,而浏览器 Javascript 则没有。你不能在 Angular 中使用它。 -
Angular 应用程序在浏览器中运行,而不是在 NodeJS 中的服务器中。您不能在 Angular 应用程序中使用文件系统。
-
@JBNizet 从技术上讲,如果您使用的是电子,则可以在浏览器实例中使用 fs。不过,情况略有不同。
-
@briosheje 从技术上讲,electron 不是浏览器,而是一个完整的桌面应用程序,它可以访问操作系统!
-
@Maryannah 是的,虽然浏览器实例实际上是一个浏览器,但只是想指定这一点,因为 OP 没有指定角度应用程序是在浏览器上运行还是在电子上的铬上运行;)。
标签: node.js angular typescript fs