typescript Angular 7 如何使用 fs 模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53656426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:43:53  来源:igfitidea点击:

Angular 7 how to use fs module?

angulartypescriptfsangular7

提问by Eng

I'm using Angular 7.

我正在使用 Angular 7。

I tried to use fs module on Typescript for open a directory. I always have this error: "Module not found: Error: Can't resolve fs" types@node and file-system are installed.

我尝试在 Typescript 上使用 fs 模块打开一个目录。我总是有这个错误:“找不到模块:错误:无法解析 fs”类型@node 和文件系统已安装。

My code:

我的代码:

import {Component, OnInit} from '@angular/core';
import * as fs from 'file-system';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit  {
  title = 'my-app';

  constructor() {

  }

  ngOnInit() {
    console.log(fs);
  }
}

Can you help me ?

你能帮助我吗 ?

Node : v10.14.0 Npm: v6.4.1 Angular CLI: v7.1.1

节点:v10.14.0 Npm:v6.4.1 Angular CLI:v7.1.1

回答by Vlad Gheorghita

The 'fs' module is a node module which is not available in Angular (or any other framework for that matter). What happens is that the Angular code is transpiled and minified and runs in the browser context, not in the node context. Browsers are sandboxed and cannot access the client file system. If this were possible than the web would be a lot more dangerous :).

'fs' 模块是一个节点模块,它在 Angular(或任何其他与此相关的框架)中不可用。发生的情况是 Angular 代码被转换和缩小并在浏览器上下文中运行,而不是在节点上下文中。浏览器被沙箱化,无法访问客户端文件系统。如果这是可能的,那么网络会更危险:)。

回答by Paque_Mann

Try this:

试试这个:

import * as fs from 'fs';

Hopefully this helps somewhat.

希望这会有所帮助。