Gremlin JavaScript Driver for Apache TinkerPop
The `gremlin` package provides the official JavaScript Gremlin Language Variant (GLV) for Apache TinkerPop, enabling developers to interact with any TinkerPop-enabled graph database. Currently stable at version 3.8.1, this driver is actively maintained with typically 3-4 releases per year, often aligning with major/minor TinkerPop versions. It differentiates itself by being the official Apache project, supporting bytecode-based traversals (the recommended approach over string-based scripts for better performance, portability, and security), and runs on Node.js (version 20 and higher) with experimental support for Web APIs. The driver establishes a WebSocket connection to a remote Gremlin Server or a compatible graph provider, translating JavaScript method calls into Gremlin traversals for execution on the server-side.
Common errors
-
Error: Server error (no request information): Invalid OpProcessor requested [null] (499)
cause This error typically indicates a mismatch in the serialization configuration between the Gremlin client and the Gremlin Server, or the client attempting an operation not supported by the server's configured OpProcessors.fixEnsure the Gremlin Server is configured with the expected serializers (e.g., GraphSON3 or GraphBinary) and that the client's `mimeType` option in `DriverRemoteConnection` matches. For specific graph providers like Azure Cosmos DB, ensure necessary authentication and specific `mimeType` settings are applied. -
WebSocket connection to 'ws://localhost:8182/gremlin' failed: WebSocket is closed before the connection is established.
cause The Gremlin Server is not running, is inaccessible at the specified URL, or a firewall is blocking the connection.fixVerify that your Gremlin Server is running and listening on the correct host and port. Check network connectivity and firewall rules. Ensure the `gremlinServerUrl` in your client code is accurate. -
TypeError: g.V is not a function
cause The `g` object (GraphTraversalSource) was not correctly initialized, or the `withRemote` method was not properly used to attach a connection.fixEnsure `g` is correctly initialized by chaining `AnonymousTraversalSource.traversal().withRemote(connection)` after establishing a `DriverRemoteConnection`. This pattern creates the fluent traversal source. -
Error: AnySerializer.deserialize(...): unknown {type_code}cause A serialization error occurred during deserialization of a response from the Gremlin Server, often indicating a data type that the client's current serializer does not understand or a mismatch in Graph Binary/GraphSON versions.fixThis can happen with complex data types or specific traversal steps (like `profile()`) not fully supported by the client's default serializer or the remote graph's capabilities. Check compatibility between your Gremlin driver version, Gremlin Server version, and specific graph database. For AWS Neptune, this error can appear with `profile()` and `explain()` steps.
Warnings
- breaking Older versions of the `gremlin` driver (pre-TinkerPop 3.3.3) did not support GraphSON3, which became the default serialization format for Gremlin Server 3.3+. This led to serialization errors. The current driver (v3.8.1) uses GraphBinary as its default serialization. Ensure your Gremlin Server and client use compatible serialization protocols.
- gotcha Gremlin operations with the `gremlin` driver are inherently transactional on the server side: each bytecode-based request constitutes a single transaction (commit on success, rollback on failure). Extending transactions over multiple requests typically requires using sessions with script-based submissions, which is generally not recommended due to portability, security, and future deprecation concerns.
- gotcha When querying AWS Neptune, the `profile()` and `explain()` traversal steps may fail or return incomplete results when using Graph Binary serialization due to Neptune's specific query optimization.
- gotcha Performing full graph scans with `g.V().count()` can be very resource-intensive on large graph databases, as it typically forces the traversal to visit all vertices across all partitions. This can lead to performance degradation.
- breaking The `gremlin` package has increased its minimum Node.js version requirement over time. As of TinkerPop 3.8.0, the minimum supported Node.js version is 20. Using older Node.js versions will result in compatibility issues.
Install
-
npm install gremlin -
yarn add gremlin -
pnpm add gremlin
Imports
- Client
import { Client } from 'gremlin';import { Client } from 'gremlin/lib/driver/client'; - DriverRemoteConnection
const DriverRemoteConnection = require('gremlin').driver.DriverRemoteConnection;import { DriverRemoteConnection } from 'gremlin/lib/driver/driver-remote-connection'; - process.AnonymousTraversalSource.traversal
import { traversal } from 'gremlin'; const g = traversal().withRemote(connection);import { AnonymousTraversalSource } from 'gremlin/lib/process/anonymous-traversal-source'; const g = AnonymousTraversalSource.traversal().withRemote(connection); - statics.P
import { P } from 'gremlin';import { statics } from 'gremlin/lib/process/traversal'; const { P } = statics;
Quickstart
import { Client } from 'gremlin/lib/driver/client';
import { DriverRemoteConnection } from 'gremlin/lib/driver/driver-remote-connection';
import { Graph } from 'gremlin/lib/structure/graph';
import { AnonymousTraversalSource } from 'gremlin/lib/process/anonymous-traversal-source';
const gremlinServerUrl = process.env.GREMLIN_SERVER_URL ?? 'ws://localhost:8182/gremlin';
async function runGremlinQuery() {
let connection: DriverRemoteConnection | null = null;
try {
// Establish a remote connection to the Gremlin Server
connection = new DriverRemoteConnection(gremlinServerUrl);
// Create a GraphTraversalSource (g) for fluent Gremlin traversals
const g = AnonymousTraversalSource.traversal().withRemote(connection);
// Execute a simple traversal: count all vertices
const vertexCount = await g.V().count().next();
console.log(`Number of vertices in the graph: ${vertexCount.value}`);
// Add a new vertex and retrieve it
const newVertex = await g.addV('person').property('name', 'Alice').next();
console.log(`Added vertex with ID: ${newVertex.value.id} and label: ${newVertex.value.label}`);
// Count all 'person' vertices
const personCount = await g.V().hasLabel('person').count().next();
console.log(`Number of 'person' vertices: ${personCount.value}`);
} catch (err) {
console.error('Error during Gremlin traversal:', err);
} finally {
if (connection) {
await connection.close();
console.log('Gremlin connection closed.');
}
}
}
runGremlinQuery();