Using Realm from npm on Electron JS app

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Using Realm from npm on Electron JS app



I am trying to use Realm imported with NPM but it fails.



I am using the Realm example for JavaScript:


const Realm = require('realm');

// Define your models and their properties
const CarSchema =
name: 'Car',
properties:
make: 'string',
model: 'string',
miles: type: 'int', default: 0,

;
const PersonSchema =
name: 'Person',
properties:
name: 'string',
birthday: 'date',
cars: 'Car',
picture: 'data?' // optional property

;

Realm.open(schema: [CarSchema, PersonSchema])
.then(realm =>
// Create Realm objects and write to local storage
realm.write(() =>
const myCar = realm.create('Car',
make: 'Honda',
model: 'Civic',
miles: 1000,
);
myCar.miles += 20; // Update a property value
);

// Query Realm for all cars with a high mileage
const cars = realm.objects('Car').filtered('miles > 1000');

// Will return a Results object with our 1 car
cars.length // => 1

// Add another car
realm.write(() =>
const myCar = realm.create('Car',
make: 'Ford',
model: 'Focus',
miles: 2000,
);
);

// Query results are updated in realtime
cars.length // => 2
)
.catch(error =>
console.log(error);
);



And this is the error it throws:



Uncaught Error: Cannot find module
'[path]/node_modules/realm/compiled/electron-v2.0_darwin_x64/realm.node'
at Module._resolveFilename (module.js:543:15)
at Function.Module._resolveFilename ([path]/node_modules/electron/dist/Electron.app/Contents/Resources/electron.asar/common/reset-search-paths.js:35:12)
at Function.Module._load (module.js:473:25)
at Module.require (module.js:586:17)
at require (internal/module.js:11:18)
at Object. ([path]/node_modules/realm/lib/index.js:102:28)
at Object. ([path]/node_modules/realm/lib/index.js:133:3)
at Module._compile (module.js:642:30)
at Object.Module._extensions..js (module.js:653:10)
at Module.load (module.js:561:32)



Thank you so much for help.




1 Answer
1



Welcome to SO!



What happens is that electron specifies its own environment, while realm runtime loads its binaries based on this currently running environment.



However, when installing realm with npm, we fetch the binaries corresponding to the environment at the time of install, i.e. our node engine.



Therefore when running electron in dev mode, realm does not find the binary corresponding to the electron environment.



The usual workaround is to use the electron-builder package and run its install-app-deps command, which will install the appropriate binaries for the electron target environment.


install-app-deps



It is usually recommended to make it an automatic script in your package.json file:


package.json



To ensure your native dependencies are always matched electron version, simply add script :


"scripts":
"postinstall": "electron-builder install-app-deps"



…so that it get run whenever you install a new package.





So I can't debug the app without install it?
– dplaza
Aug 6 at 7:38





The mentioned "install" is for your realm package, not your app.
– ghybs
Aug 6 at 7:53


realm





Now it works even when I run without installation. Why?
– dplaza
Aug 6 at 7:56





I mean: I thought that Realm only works if I build a package with electron-builder. But it seems that it works too when I call to npm start
– dplaza
Aug 6 at 7:57





Once you run the electron-builder install-app-deps command, you download the realm binary for your electron version. Then even in electron dev mode (npm start) realm can find the appropriate binary.
– ghybs
Aug 6 at 8:04


electron-builder install-app-deps


npm start






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard