dpl*_*aza 3 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);
});
Run Code Online (Sandbox Code Playgroud)
这是它抛出的错误:
未捕获错误:在 Function.Module._resolveFilename ([path]) 的 Module._resolveFilename (module.js:543:15) 处找不到模块 '[path]/node_modules/realm/compiled/electron-v2.0_darwin_x64/realm.node' /node_modules/electron/dist/Electron.app/Contents/Resources/ Electron.asar/common/reset-search-paths.js:35:12) 在 Function.Module._load (module.js:473:25) 在模块.require (module.js:586:17) at require (internal/module.js:11:18) at Object. ([path]/node_modules/realm/lib/index.js:102:28) 在对象处。([path]/node_modules/realm/lib/index.js:133:3) 在 Module._compile (module.js:642:30) 在 Object.Module._extensions..js (module.js:653:10)在 Module.load (module.js:561:32)
非常感谢您的帮助。
欢迎来到SO!
\n\n发生的情况是,电子指定了自己的环境,而领域运行时则根据当前运行的环境加载其二进制文件。
\n\n然而,当使用npm安装realm时,我们会获取安装时与环境相对应的二进制文件,即我们的节点引擎。
\n\n因此,当以dev模式运行Electron时,Realm找不到Electron环境对应的二进制文件。
\n\n通常的解决方法是使用electro-builder包并运行其install-app-deps命令,这将为 Electron 目标环境安装适当的二进制文件。
通常建议将其设为文件中的自动脚本package.json:
\n\n\n为了确保您的本机依赖项始终与电子版本匹配,只需添加脚本:
\n
"scripts": {\n "postinstall": "electron-builder install-app-deps"\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\xe2\x80\xa6,以便每当您安装新软件包时它都会运行。
\n| 归档时间: |
|
| 查看次数: |
1665 次 |
| 最近记录: |