realm - 在 Electron JS 应用程序上使用 npm 中的 Realm

标签 realm electron npm-install

我正在尝试使用通过 NPM 导入的 Realm,但它失败了。

我正在为 JavaScript 使用 Realm 示例:

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

这是它抛出的错误:

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)

非常感谢您的帮助。

最佳答案

欢迎来到 SO!

发生的事情是 electron 指定了它自己的环境,而 realm 运行时根据当前运行的环境加载它的二进制文件。

但是,当使用 npm 安装 realm 时,我们会在安装时获取与环境对应的二进制文件,即我们的节点引擎。

因此在dev模式下运行electron时,realm并没有找到electron环境对应的binary。

通常的解决方法是使用 electron-builder打包并运行其 install-app-deps 命令,这将为 Electron 目标环境安装适当的二进制文件。

usually recommended使其成为您的 package.json 文件中的自动脚本:

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

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

...以便它在您安装新软件包时运行。

关于realm - 在 Electron JS 应用程序上使用 npm 中的 Realm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51701693/

相关文章:

angular - 从不同的仓库导入组件

swift - 如何将 List 类型与 Codable 一起使用? ( Realm swift )

Swift Realm 通过列表属性元素查询

node.js - 使用 Node.js 将窗口设置为节能模式

electron - 如何使用appimage为raspbian构建和安装 Electron 应用程序

angular - 从 2 更新到 Angular 4。@Angular/core 和 @Angular/http 的 "Unmet Peer Dependencies"

android - 无法在 Realm.io 中处理 RealmList<RealmList<RealmInt>>

iOS - 将元素附加到 Realm 中的列表不会保留元素

electron - 从 Electron 应用程序获取 Azure Active Directory token

react-native - npm 安装时遇到代码 EWORKSPACESCONFIG 问题