Rodrigo Ramos
Rodrigo Ramos

Reputation: 17

node.js cannot find 'neo4j' module (Thingdom)

After many tries, I am unnable to connect node.js to Neo4j installed in my computer. I am able to access both separately, and both work fine. I have install in my Node.js directory the Thingdom ('neo4j') module in the directory, but when require('neo4j') prints an error.

Image of my Node.js folder with Neo4j installed in modules

var neo4j = require("neo4j");
var db = new neo4j.GraphDatabase("http://localhost:7474");

var node = db.createNode({hello: 'world'});     // instantaneous, but...
node.save(function (err, node) {    // ...this is what actually persists.
    if (err) {
        console.error('Error saving new node to database:', err);
    } else {
        console.log('Node saved to database with id:', node.id);
    }
});

And when using in the cmd: "node index.js" it throws me this error:

C:\Users\RRamos\Documents\Projects\test-neo4j>node index.js
module.js:341
    throw err;
    ^

Error: Cannot find module 'neo4j'
    at Function.Module._resolveFilename (module.js:339:15)
    at Function.Module._load (module.js:290:25)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (C:\Users\RRamos\Documents\Projects\test-neo4j\index.js:1:75)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)

Upvotes: 1

Views: 1315

Answers (1)

JavaNoScript
JavaNoScript

Reputation: 2413

I got the same problem. As there's no solution in this post, I just add mine.

After running $ npm init and $ npm install --save neo4j-driver, I copy and paste the neo4j example code in index.js:

var neo4j = require("neo4j");
var db = new neo4j.GraphDatabase('http://neo4j:<password>@localhost:7474');

And then I got the same error when running $ node index.js.

In my package.json, I found:

"dependencies": {
    "neo4j-driver": "^1.1.0-M02"
}

It's neo4j-driver not neo4j. So replace it in index.js:

var neo4j = require("neo4j-driver");
var db = new neo4j.GraphDatabase('http://neo4j:<password>@localhost:7474');

Now you will get rid of the Cannot find module 'neo4j' error!


In addition, if you use the 1.1.0 version of neo4j-driver(for Neo4j 3.0.0+), you may get this error:

var db = new neo4j.GraphDatabase('http://neo4j:<password>@localhost:7474');
         ^

TypeError: neo4j.GraphDatabase is not a constructor
    at Object.<anonymous> (D:\Codes\neo4j_test\server.js:2:10)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.runMain (module.js:575:10)
    at run (bootstrap_node.js:352:7)
    at startup (bootstrap_node.js:144:9)
    at bootstrap_node.js:467:3

It seems neo4j.GraphDatabase is only available in older version of neo4j-driver.

Here's the up-to-date tutorial of neo4j-driver.

Use the following code instead:

var neo4j = require('neo4j-driver').v1;
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "<password>"));

// Create a session to run Cypher statements in.
// Note: Always make sure to close sessions when you are done using them!
var session = driver.session();

// Run a Cypher statement, reading the result in a streaming manner as records arrive:
session
  .run("MERGE (alice:Person {name : {nameParam} }) RETURN alice.name", { nameParam:'Alice' })
  .subscribe({
    onNext: function(record) {
     console.log(record._fields);
    },
    onCompleted: function() {
      // Completed!
      session.close();
    },
    onError: function(error) {
      console.log(error);
    }
  });

Upvotes: 1

Related Questions