neo4j / neo4j-javascript-driver

Neo4j Bolt driver for JavaScript
https://neo4j.com/docs/javascript-manual/current/
Apache License 2.0
854 stars 148 forks source link

Unable to connect to Neo4j from nodejs using neo4j-driver #639

Closed AtharvaThorve closed 3 years ago

AtharvaThorve commented 3 years ago

The connection between the Neo4j server and the neo4j-driver is not being established.

Neo4j Version: 4.1.0 Enterprise via Neo4j Desktop 1.3.10 Neo4j Mode: Single instance(?) Driver version: ^4.1.2 Operating System: Windows 10 Home

Steps to reproduce

I tried to test neo4j-driver with the code example given in the docs.

index.js

const neo4j = require('neo4j-driver');

const driver = neo4j.driver(
    'bolt://localhost',
    neo4j.auth.basic('neo4j', 'test')
);
const session = driver.session();
const personName = 'Alice';

const addData = async () => {
    try {
        const result = await session.run(
            'CREATE (a:Person {name: $name}) RETURN a',
            { name: personName }
        );

        const singleRecord = result.records[0];
        const node = singleRecord.get(0);

        console.log(node.properties.name);
    } catch (err) {
        console.log(err);
    } finally {
        await session.close();
    }

    // on application exit:
    await driver.close();
};

addData();

I'm starting my Neo4j instance from Neo4j Desktop. The following is the log output when the instance starts:

2020-11-16 07:17:16.570+0000 INFO  ======== Neo4j 4.1.0 ========
2020-11-16 07:17:24.397+0000 INFO  Sending metrics to CSV file at C:\Users\aaath\AppData\Local\Neo4j\Relate\Data\dbmss\dbms-08df0cd5-4329-4d1d-8f79-5d211340d255\metrics
2020-11-16 07:17:24.443+0000 INFO  Bolt enabled on localhost:7687.
2020-11-16 07:17:26.788+0000 INFO  Remote interface available at http://localhost:7474/
2020-11-16 07:17:26.792+0000 INFO  Started.

Expected behavior

Running node index.js should result in: Alice being printed on the console.

Actual behavior

Running node index.js results in:

Neo4jError: Failed to connect to server. Please ensure that your database is listening on the correct host and port and that you have compatible encryption settings both on Neo4j server and driver. Note that the default encryption setting has changed in 
Neo4j 4.0. Caused by: connect ECONNREFUSED 127.0.0.1:7687

    at captureStacktrace (/mnt/d/Codes/Neo4j Driver Test/node_modules/neo4j-driver/lib/result.js:277:15)
    at new Result (/mnt/d/Codes/Neo4j Driver Test/node_modules/neo4j-driver/lib/result.js:68:19)
    at Session._run (/mnt/d/Codes/Neo4j Driver Test/node_modules/neo4j-driver/lib/session.js:174:14)
    at Session.run (/mnt/d/Codes/Neo4j Driver Test/node_modules/neo4j-driver/lib/session.js:135:19)
    at addData (/mnt/d/Codes/Neo4j Driver Test/index.js:12:32)
    at Object.<anonymous> (/mnt/d/Codes/Neo4j Driver Test/index.js:32:1)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14) {
  code: 'ServiceUnavailable',
  name: 'Neo4jError'
}
bigmontz commented 3 years ago

Hi @AtharvaThorve,

could you check if the server is listening on the correct port by running the query CALL dbms.listConfig() YIELD name, value WHERE name IN ['dbms.connector.bolt.enabled', 'dbms.connector.bolt.listen_address'] RETURN name, value on the Neo4j Desktop?

If it's listening, You could also check if the windows firewall is not blocking the connection.

AtharvaThorve commented 3 years ago

@bigmontz When I executed the command CALL dbms.listConfig() YIELD name, value WHERE name IN ['dbms.connector.bolt.enabled', 'dbms.connector.bolt.listen_address'] RETURN name, value I got this output:

image

And both McAfee antivirus and windows defender firewall has not shown any blocked connection

bigmontz commented 3 years ago

@AtharvaThorve,

could you try to connect to the server running telnet localhost 7687 and checking if it connects? You could also run netstat -a -n to check in which interface Neo4j is listening.

AtharvaThorve commented 3 years ago

@bigmontz I executed both the commands The output of netstat -a -n was (all the highlighted part is of the port 7687): Select C__windows_system32_cmd exe 16-11-2020 20_06_28

When I executed telnet localhost 7687 command I got a blank screen with telnet running.

2hdddg commented 3 years ago

@AtharvaThorve can you try to replace bolt://localhost with bolt://127.0.0.1 instead? Just to make sure that name resolving works as expected. EDIT: Don't bother, I can see that it resolved ok in the error message

bigmontz commented 3 years ago

@AtharvaThorve, which version of NodeJS are you using?

AtharvaThorve commented 3 years ago

@bigmontz The node version I am using is 12.14.1

bigmontz commented 3 years ago

@AtharvaThorve

I've tried to reproduce the error here, but I couldn't. Are you running the code using node script.js over the cmd? Could you try with the firewalls disabled?

AtharvaThorve commented 3 years ago

@bigmontz No, actually I was using wsl 2 setup but when I switched to windows powershell it executed properly. I still don't understand why it wasn't executing properly in wsl though. Thank you for all your help 🙌.

ghost commented 1 year ago

Did this get resolved? @AtharvaThorve