Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node module not working in react and electron implementation

Tags:

I am building a simple electron app using react, i needed to use the os.homedir method to get the home directory of the user.

Here is my react component,

import React, { Component } from 'react'
import Os from 'os'

export default class Item extends Component {

  constructor( props ) {
    super( props )
  }

  render () {
    return (
      <div className='item-component'>
        { Os.homedir() }
      </div>
    )
  }

}

But seems that electron is not utilizing the os module of node js. Thus an

Uncaught TypeError: _os2.default.homedir is not a function

error occures.

Instead of importing the os module i also used the require method with no luck.

let Os = require( 'os' )

How to use node modules in electron and react setup?

Added Project to GitHub https://github.com/rakibtg/ElectronThisIsReact

You can clone it to test ....

like image 935
rakibtg Avatar asked Dec 05 '16 18:12

rakibtg


1 Answers

I know this is like 4 months late... but if it helps now try using let Os = window.require( 'os' ).

In my app that i'm developing using react and electron if i did not say window.require I would get the same error.

like image 115
keziakoko Avatar answered Sep 25 '22 16:09

keziakoko