Cannot find module realm.node when from ES6 or CJS module

I cannot obtain a reference to the realm module from nodejs when either a ES6 or CJS module type.

repro:

  1. create new folder
  2. npm install realm
  3. create new javascript file
  4. add a require or import statement for realm
  5. run & it fails

Error

  return process.dlopen(module, path.toNamespacedPath(filename));
                 ^

Error: The specified module could not be found.
.......\node_modules\realm\build\Release\realm.node

Fails:

import { createRequire } from "module";
const require = createRequire(import.meta.url);
const Realm = require('realm');

Also fails:

import * as Realm from 'realm';

Also fails:

const realm = require('realm');

node version is:
v16.13.1
realm: 10.21.1

1 Like

The realm code that is failing is lib/index.js

// Prevent React Native packager from seeing modules required with this
const nodeRequire = require;

function getRealmConstructor(environment) {
  switch (environment) {
    case "node.js":
    case "electron":
     ----> return nodeRequire("bindings")("realm.node").Realm;

Turns out that if I downgrade realm to 10.20.0, then I can get a reference to realm. So something broke starting in 10.21.0. @Ian_Ward

This is on windows 10, v16.13.1.

2 Likes

The same happens on my Windows 11

import Realm from "realm";
console.log(Realm)
Error: The specified module could not be found.
\\?\C:\Users\C\AppData\Roaming\npm\node_modules\realm\build\Release\realm.node
    at Object.Module._extensions..node (node:internal/modules/cjs/loader:1203:18)
    at Module.load (node:internal/modules/cjs/loader:997:32)
    at Function.Module._load (node:internal/modules/cjs/loader:838:12)
    at Module.require (node:internal/modules/cjs/loader:1021:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at bindings (C:\Users\C\AppData\Roaming\npm\node_modules\realm\node_modules\bindings\bindings.js:112:48)
    at getRealmConstructor (C:\Users\C\AppData\Roaming\npm\node_modules\realm\lib\index.js:28:37)
    at Object.<anonymous> (C:\Users\C\AppData\Roaming\npm\node_modules\realm\lib\index.js:53:26)
    at Module._compile (node:internal/modules/cjs/loader:1119:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1173:10) {
  code: 'ERR_DLOPEN_FAILED'
}

Did anyone find a solution for this?

I’m building an electron and Web app using Realm. In my electron app (which apparently wants me to use require - an electron issue - and not use import / esm), I can’t seem to use Realm = require(‘realm’)…

Any ideas?

I had a mix of import / require in my project. When I removed all the imports and used the following for Realm, it works in Node / Electron:

const Realm = require(‘realm’);