【问题标题】:node.js cannot find 'neo4j' module (Thingdom)node.js 找不到“neo4j”模块(Thingdom)
【发布时间】:2016-03-07 15:36:24
【问题描述】:

经过多次尝试,我无法将 node.js 连接到我计算机中安装的 Neo4j。我可以分别访问两者,并且都可以正常工作。我已经在我的 Node.js 目录中安装了 Thingdom ('neo4j') 模块,但是当 require('neo4j') 打印错误时。

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);
    }
});

当在 cmd: "node index.js" 中使用时,它会抛出这个错误:

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)

【问题讨论】:

  • 看起来好像模块是作为全局安装的,这不好。为项目在本地安装。
  • 我认为是安装在项目内部。您可以在图像中查看它(我的 Node.js 文件夹的图像,其中 Neo4j 安装在模块中)。
  • 没错!我明白了!,现在看来它可以工作了
  • 我遇到了同样的问题,我在web/ 目录下安装了neo4j-driver,但它应该更高一级,与我的node.js 服务器代码在同一目录中。

标签: javascript node.js neo4j


【解决方案1】:

我遇到了同样的问题。由于这篇文章没有解决方案,我只是添加我的。

运行$ npm init$ npm install --save neo4j-driver后,我将neo4j example code复制并粘贴到index.js中:

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

然后我在运行$ node index.js时遇到了同样的错误。

在我的 package.json 中,我发现:

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

这是neo4j-driver 而不是neo4j。所以在 index.js 中替换它:

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

现在您将摆脱Cannot find module 'neo4j' 错误!


另外,如果你使用1.1.0版本的neo4j-driver(for Neo4j 3.0.0+),可能会出现这个错误:

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

似乎neo4j.GraphDatabase 仅在旧版本的 neo4j-driver 中可用。

这里是the up-to-date tutorial of neo4j-driver

请改用以下代码:

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);
    }
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 2013-08-10
    • 2019-05-12
    • 2012-01-02
    • 2017-02-18
    • 2012-12-22
    • 1970-01-01
    相关资源
    最近更新 更多