Navigation

Install Realm for Node.js

Overview

The MongoDB Realm Node.js SDK enables development of applications using the JavaScript and TypeScript languages. MongoDB Realm on Node.js is best suited for writing server-side applications. Your Node.js application could be one component in a wider stack that also includes iOS and Android apps.

The MongoDB Realm Node.js SDK is not suitable for front-end application development.

  • Due to limitations of the browser environment, it is not possible to build a browser-based web app with this SDK. For front-end web applications, use the MongoDB Realm GraphQL SDK.
  • For cross-platform mobile app development using web frameworks, use the MongoDB Realm React Native SDK.
  • MongoDB Realm does not officially support other front-end frameworks, such as Electron.

Prerequisites

Before getting started, ensure your environment meets the following prerequisites:

  • Node.js version 10.x or later (including Node version 14)
  • Linux, macOS 10.8 (or later), or Windows 8 (or later)

Installation

Follow these steps to create a Node.js project and add the MongoDB Realm Node.js SDK to it.

1

Create a Node.js Project

Create your Node.js project by creating a new directory for your project and running npm init in that directory. In the example below, replace MyRealmApp with your desired project name. Answer all of the prompts to fill out the details of your project.

mkdir MyRealmApp && cd MyRealmApp && npm init
2

Install Realm with NPM

In your Node.js project directory, use the following command to add Realm to your project:

npm install --save realm@10.0.0-beta.6
3

Enable TypeScript (optional)

TypeScript is a superset of JavaScript that adds static type checking and other features intended to make application-scale development more robust. If you’d like to use TypeScript, follow the TypeScript team’s official Node Starter guide. Realm supports TypeScript natively and integrates easily into a TypeScript project.

Import Realm

Add the following line to the top of your source files where you want to use MongoDB Realm:

import Realm from "realm";
const Realm = require("realm");