Matteo Meil
Matteo Meil

Reputation: 1353

Using node's fs results in empty object

I'm building a simple project with Angular 6 and Electron 4. In my project I'd want to use fs from node but it results in an empty object when I try to output it. Here's my code:

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
  }
}

I've added "browser": {"fs": false} as suggested in this issue, and I've installed @types/node and added "types": ["./../node_modules/@types/node"] in tsconfig.app.json.

I googled a lot to find a solution but I can't get it to work. Any suggestions?

Upvotes: 0

Views: 1619

Answers (2)

Matteo Meil
Matteo Meil

Reputation: 1353

Finally found a solution to use Node.js API in Angular 6.

Installed electron-builder and follwed the instruction, I can now use regular import statement.

The configuration I mentioned above helps to suppress errors like Module 'fs' not found and to help IDEs to recognize fs module

Upvotes: 1

user1230234
user1230234

Reputation: 34

fs module uses the os and path under the hood. According to this answer you need to assign false to all of them in the config object.

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

Upvotes: 0

Related Questions