Tamas Tamas - 1 year ago 190
TypeScript Question

How to reference node fs API with electron in typescript?

I've created an electron app based on this link (but using recent modules).

I want to create a simple desktop app, I need to open some local files. I know that there is the node fs API for this, but I'm have problems with referencing it correctly.

Here is what I tried:

  • Based on this queston in Stackoverflow, I tried to import and use fs like this:

    ///<reference path="../typings/node/node.d.ts"/>
    import fs = require('fs');

    As a result, I got a Module not found: Error: Cannot resolve module 'fs' when I called npm run build

  • Then I added the following lines to webpack.config.js (I read it somewhere):

    node: {
    fs: "empty"

    then npm run build command was OK, but I see the following error at runtime in electron's developer tool: TypeError: fs.readFileSync is not a function

I'm new to this topic, and I would appreciate if someone could help me how to reference node's fs API correctly.

Answer Source

You need to set target: 'electron-renderer' in your Webpack config, if you still have issues after that take a look at https://github.com/chentsulin/electron-react-boilerplate

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download