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

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)

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

Related

Update JSON file write not working in NodeJS

I'm trying to read existing JSON file and update the existing value and saving it back. I'm getting below error message:
node:internal/modules/cjs/loader:944
throw err;
^
Error: Cannot find module 'src/myApp/test.json'
Require stack:
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:941:15)
at Function.Module._load (node:internal/modules/cjs/loader:774:27)
at Module.require (node:internal/modules/cjs/loader:1013:19)
at require (node:internal/modules/cjs/helpers:93:18)
at Module._compile (node:internal/modules/cjs/loader:1109:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
at Module.load (node:internal/modules/cjs/loader:989:32)
at Function.Module._load (node:internal/modules/cjs/loader:829:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12) {
code: 'MODULE_NOT_FOUND',
}
Please find the code below -
Much appreciated if someone can help me to fix this issue.
test.json
{
"key": ""
}
updateJSON.js
const fs = require('fs');
const fileName = 'src/myApp/test.json';
const file = require(fileName);
file.key = "new value";
fs.writeFile(fileName, JSON.stringify(file), function writeJSON(err) {
if (err) return console.log(err);
console.log(JSON.stringify(file));
console.log('writing to ' + fileName);
});
The error message Error: Cannot find module 'src/myApp/test.json' indicates that the Node.js cannot resolve the src/myApp/test.json file correctly.
By default, the name(src/myApp/test.json here) not starting with ./ will be recognized as a module of Node.js and will be resolved from native modules and node_modules, which leads to the error.
Using a relative path like ../src/myApp/test.json should fix it.
Ref: https://nodejs.org/api/modules.html#modules_require_id

Error: Cannot find module 'config' at Function.Module._resolveFilename

Hii I have simple node server, with the following structure
myapp
-config
-default-json
-index.js
-package-lock.json
-package.json
Here is my part of my index.js
'use strict';
const
config = require('config'),
express = require('express'),
request = require('request'),
body_parser = require('body-parser'),
app = express().use(body_parser.json()); // creates express http server
// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log('webhook is listening'));
when I run node index.js I get the following error
internal/modules/cjs/loader.js:583
throw err;
^
Error: Cannot find module 'config'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (C:\xampp\htdocs\chat\index.js:13:14)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
what is wrong with my code?
I found solution by by installing config from npm
https://www.npmjs.com/package/config
follow the instruction above and it should work , it might help some one in future
You have to explicitly add the config module in your package.json:
"dependencies": {
"config": "version number"
}
https://www.npmjs.com/package/config
It means there is no config.js in your current location. Put the exact location of the config.js file..
Try,
config = require('./config/config'),

Detox issue: BUILD FAILED Ld build/Build/Products/Debug-iphonesimulator

I'm trying to use Wix/Detox to test my react-native app (the iOS version). I have successfully followed the instructions (up until "detox build") at https://github.com/wix/detox/blob/master/docs/Introduction.GettingStarted.md
However, when running "detox build" in my project directory, I get the following error:
** BUILD FAILED **
The following commands produced analyzer issues:
Analyze RNFIRMessaging.m
(1 command with analyzer issues)
The following build commands failed:
Ld build/Build/Products/Debug-iphonesimulator/<myAppName>.app/<myAppName> normal x86_64
(1 failure)
child_process.js:524
throw err;
^
Error: Command failed: xcodebuild -project ios/<myAppName>.xcodeproj -scheme <myAppName> -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build
at checkExecSyncError (child_process.js:481:13)
at Object.execSync (child_process.js:521:13)
at Object.<anonymous> (/Users/<myUserName>/Documents/react-native-projects/<myAppName>/node_modules/detox/local-cli/detox-build.js:26:6)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:423:7)
When running "detox test," I get the following error:
node_modules/.bin/mocha e2e --opts e2e/mocha.opts
detox info 09:31:22: server listening on localhost:50342...
detox info 1: Listing devices...
1) "before all" hook
0 passing (376ms)
1 failing
1) "before all" hook:
Error: field CFBundleIdentifier not found inside Info.plist of app binary at /Users/<myUserName>/Documents/react-native-projects/<myAppName>/ios/build/Build/Products/Debug-iphonesimulator/<myAppName>.app
at SimulatorDriver._callee2$ (node_modules/detox/lib/devices/SimulatorDriver.js:28:19)
at tryCatch (node_modules/regenerator-runtime/runtime.js:64:40)
at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:299:22)
at Generator.prototype.(anonymous function) [as throw] (node_modules/regenerator-runtime/runtime.js:116:21)
at step (node_modules/detox/lib/devices/SimulatorDriver.js:1:809)
at node_modules/detox/lib/devices/SimulatorDriver.js:1:1008
child_process.js:524
throw err;
^
Error: Command failed: node_modules/.bin/mocha e2e --opts e2e/mocha.opts
at checkExecSyncError (child_process.js:481:13)
at Object.execSync (child_process.js:521:13)
at Object.<anonymous> (/Users/<myUserName>/Documents/react-native-projects/<myAppName>/node_modules/detox/local-cli/detox-test.js:46:4)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:423:7)
I have an e2e folder in my project directory with three files: init.js, mocha.opts, and firstTest.spec.js.
firstTest.spec.js looks like this:
/* eslint-env node, mocha */
/* global element, expect, device, by */
describe('FiestTest', () => {
beforeEach(async () => {
await device.reloadReactNative()
})
it('Shoudl find the phoneInput field and the init value as "09"', async () => {
await expect(element(by.id('LoginPage-phoneInput'))).toBeVisible()
await element(by.id('LoginPage-phoneInput')).typeText('09991234567')
await expect(element(by.id('LoginPage-loginGuideTxt'))).toBeVisible()
await element(by.id('LoginPage-loginBtn')).tap()
})
it('Dummy1"', async () => {
await device.reloadReactNative()
})
it('Dummy2', async () => {
await device.reloadReactNative()
})
it('Dummy3', async () => {
await device.reloadReactNative()
})
})
init.js looks like this:
/* eslint-env node, mocha */
require('babel-polyfill')
const detox = require('detox')
const config = require('../package.json').detox
before(async () => {
await detox.init(config)
})
after(async () => {
await detox.cleanup()
})
And mocha.opts looks like this:
--recursive --timeout 120000 --bail
Your help is highly appreciated!
I ran the following command and it solved the build issue:
watchman watch-del-all; npm start -- --reset-cache
This solved the issue for me:
detox clean-framework-cache && detox build-framework-cache
I have faced this issue while implementing detox in my react native project.
I have fixed it just by using latest fixing the project.pbxproj file
basically fixing the license configuration.

EPERM fs.rename

I am coming up with an error for this node script I wrote to rename a bunch of files.
> node test/rename.js
fs.js:809
return binding.rename(pathModule._makeLong(oldPath),
^
Error: EPERM: operation not permitted, rename '/path/to/file v2 032.png' -> '/path/to/file 032.png'
at Error (native)
at Object.fs.renameSync (fs.js:809:18)
at Object.<anonymous> (/Users/[user]/Documents/test/rename.js:9:8)
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)
This is the code I am working with.
var fs = require('fs');
var path = require('path');
var folder = '/path/to/'
var regExp = /\sv2/gi;
var directory = fs.readdirSync(folder);
for (i=0; i < directory.length; i++) {
if (directory[i].match(regExp)) {
fs.renameSync(path.join(folder, directory[i]), path.join(folder, directory[i].replace(regExp, '')))
};
}
I tried it with as both node test/rename.js and sudo node test/rename.js with both throwing back errors. I don't see where I goofed up.
Solution: Files were locked within OSX's Get Info tab. I unlocked the files, re-ran the node script, and vola! It twerked and it worked.

nodeJS phantom ReferenceError: Promise is not defined

Problem
I installed phantom and using node v0.10.26
I am getting a Promise error within phantom. I did not see any dependencies that needed to be installed.
Below is the sample code from phantom that I am using
/myProject/node_modules/phantom/lib/index.js:15
return new Promise(function (resolve) {
^
ReferenceError: Promise is not defined
at Object.module.exports.create (/Users/jbyrne/git/tin-validator/node_modules/phantom/lib/index.js:15:14)
at Object. (/Users/jbyrne/git/tin-validator/phantom.js:6:9)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
I upgraded node to v0.11.13 and now get
$ node phantom.js
Exception: ReferenceError: Map is not defined
I installed phantom and using node v0.10.26
I am getting a Promise error within phantom. I did not see any dependencies that needed to be installed.
Below is the sample code from phantom that I am using
/myProject/node_modules/phantom/lib/index.js:15
return new Promise(function (resolve) {
^
ReferenceError: Promise is not defined
at Object.module.exports.create (/Users/jbyrne/git/tin-validator/node_modules/phantom/lib/index.js:15:14)
at Object. (/Users/jbyrne/git/tin-validator/phantom.js:6:9)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
I upgraded node to v0.11.13 and now get
$ node phantom.js
Exception: ReferenceError: Map is not defined
Code
var phantom = require('phantom');
var sitepage = null;
var phInstance = null;
phantom.create()
.then(function(instance) {
phInstance = instance;
return instance.createPage();
})
.then(function(page) {
sitepage = page;
return page.open('https://stackoverflow.com/');
})
.then(function(status) {
console.log(status);
return sitepage.property('content');
})
.then(function(content) {
console.log(content);
sitepage.close();
phInstance.exit();
})["catch"](function(e) {
console.error('Exception: %s', e);
});
Your version of PhantomJS depends on some global objects that don't exist in Node 0.10. You can either upgrade Node to the latest stable version, or downgrade your PhantomJS to 1.x.
If you're switching between Node versions, you can do so easily with nvm.
upgrade node to 0.12
the error mentioned is due to the line new Map() in phantom.
the Map constructor was added in node 0.12

Categories

Resources