javascript - node.js 找不到 'neo4j' 模块 (Thingdom)

标签 javascript node.js neo4j

经过多次尝试,我无法将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)

最佳答案

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

运行 $ npm init$ npm install --save neo4j-driver 后,我复制并粘贴 neo4j example codeindex.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');

现在您将摆脱无法找到模块“neo4j”错误!

<小时/>

此外,如果您使用1.1.0版本的neo4j-driver(适用于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);
    }
  });

关于javascript - node.js 找不到 'neo4j' 模块 (Thingdom),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35847731/

相关文章:

javascript - 嵌套异步函数的问题(异步系列中的异步 waterfall )

neo4j - Neo4j 的水平可扩展性项目 Rassilon 处于什么状态?

javascript - HTML 加粗不呈现

javascript - 这两个状态更新在 react 中有什么区别?

javascript - jquery被选择

javascript - Node.js 创建的服务器在首页加载时发出 3 个请求,在每次页面刷新后发出 2 个请求?

javascript - Lodash union 无法与展开操作符一起使用

node.js - 如何在不保持图像比例的情况下调整图像大小?

java - 在 Neo4j 中如何创建触发器?

java - 从 Java 代码执行 Cypher 查询时出现空异常错误。 (Neo4J)