Accessing filesystem in Angular 2 app using Electron

前端 未结 5 1266
抹茶落季
抹茶落季 2021-01-02 11:00

I know that Angular 2 is run on a web browser, which does not have access to the file system.

However, I\'m using Electron as my front-end, and also running the app

5条回答
  •  再見小時候
    2021-01-02 11:42

    I am using

    Angular CLI: 7.0.7
    Node: 8.12.0
    OS: win32 x64
    Angular: 7.0.4
    

    I tried the ng eject method it didn't work in my case, it is disabled by default and will be removed completely in Angular 8.0

    Error message: The 'eject' command has been disabled and will be removed completely in 8.0.

    It worked for me by creating a file called native.js in the src folder and insert the following:

    `window.fs = require('fs');
    

    Add this file to the angular-cli.json scripts array:

    "scripts": [
        "native.js"
    ]
    

    Add the following lines to polyfills.ts:

    `declare global {
        interface Window {
            fs: any;
        }
    }`
    

    After that you can access the filesystem with:

    `window.fs.writeFileSync('sample.txt', 'my data');`
    

    credits

提交回复
热议问题