使用节点的fs导致空对象

时间:2019-01-27 15:37:15

标签: node.js angular electron

我正在用Angular 6和Electron 4建立一个简单的项目。 在我的项目中,我想使用node中的fs,但是当我尝试输出它时会导致一个空对象。这是我的代码:

import { Component } from '@angular/core';
import * as fs from 'fs';

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

  constructor() {
    console.log(fs); // empty object
  }
}

我已按照this问题中的建议添加了"browser": {"fs": false},并安装了@types/node并在"types": ["./../node_modules/@types/node"]中添加了tsconfig.app.json

我在Google上进行了大量搜索以找到解决方案,但无法使其正常工作。有什么建议吗?

2 个答案:

答案 0 :(得分:1)

最后找到了在Angular 6中使用Node.js API的解决方案。

已安装electron-builder并按照说明进行操作,现在我可以使用常规的import语句。

我上面提到的配置有助于抑制诸如Module 'fs' not found之类的错误,并有助于IDE识别fs模块

答案 1 :(得分:0)

fs模块在​​内部使用ospath。 根据此answer,您需要在config对象中为所有它们分配false。

"browser": {
    "fs": false,
    "path": false,
    "os": false
  }