经过多次尝试,我无法将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 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'
错误!另外,如果您使用neo4j-driver(对于Neo4j 3.0.0+)的1.1.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驱动程序中可用。这是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/