How can I create a directory in user home using nodejs? - node.js

I am developing a node.js application on Ubuntu and am trying to programmatically create a directory for my application in the user's home directory.
When I execute the following Javascript in Node:
const fs = require("fs");
fs.mkdirSync("~/mynewdir");
I get the following error:
Error: ENOENT: no such file or directory, mkdir '~/mynewdir'
at Error (native)
at Object.fs.mkdirSync (fs.js:923:18)
at repl:1:4
at sigintHandlersWrap (vm.js:22:35)
at sigintHandlersWrap (vm.js:73:12)
at ContextifyScript.Script.runInThisContext (vm.js:21:12)
at REPLServer.defaultEval (repl.js:346:29)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:545:10)
Executing:
fs.mkdirSync("/home/dah/mynewdir");
works just fine however, but I want to use the home directory of whomever is executing the script.
Does anyone have any suggestions?
Edit - this question is not a duplicate. In this case, the issue is not finding the home directory (I already have this), but why the fs module won't use it.

You can do it like that:
const homedir = require('os').homedir();
// `homedir()` returns absolute path so we use `join` here
require("fs").mkdir(require('path').join(homedir, 'mynewdir'));

Related

EACCES: permission denied with Node JS

I get below error when write a file (file name is book) with Node.js, could you please help?
Error: EACCES: permission denied, open '/book'
at Object.openSync (fs.js:443:3)
at Object.writeFileSync (fs.js:1163:35)
at Object.<anonymous> (/home/ubuntu/remoteserver/ionicappGate.js:375:6)
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)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:266:19)
The code is as below
const fs = require('fs');
const path = "/book";
//do whatever required after initialize
fs.writeFileSync(path, "hello book");
app.use("/", router);
app.listen(4000, () => console.log('Platform Server running on port 4000'))
You're trying to write to the root of your file system "/book". This is probably write protected (default in Linux). If you really mean to write to that directory, check to make sure the user running the node process has write permissions to that folder. Otherwise, change to the path relative to the script such as ./book and again make sure the user running the node process has write permissions to that folder.
I hope the script command below may resolve your problem:
chmod -R 755 book/*
Try to check permissions to file with fs.access(path[, mode], callback).
Also check your folder permissions. Read more detail about file system permissions here

Starting hyperledger explorer fails

I have installed all prerequisites for setting up the hyperledger Explorer but when I start it, I got the following error in log file:
postgres://hppoc:password#127.0.0.1/fabricexplorer
fs.js:675
return binding.read(fd, buffer, offset, length, position);
^
Error: EISDIR: illegal operation on a directory, read
at Object.fs.readSync (fs.js:675:18)
at tryReadSync (fs.js:540:20)
at Object.fs.readFileSync (fs.js:583:19)
at configuration.getPeersByOrg.forEach.key (/home/ubuntu/blockchain-explorer/app/FabricClientProxy.js:164:20)
at Array.forEach (<anonymous>)
at FabricClientProxy.setupPeers (/home/ubuntu/blockchain-explorer/app/FabricClientProxy.js:161:36)
at configuration.getOrgs.forEach.key (/home/ubuntu/blockchain-explorer/app/FabricClientProxy.js:155:10)
at Array.forEach (<anonymous>)
at FabricClientProxy.createDefault (/home/ubuntu/blockchain-explorer/app/FabricClientProxy.js:137:27)
at new FabricClientProxy (/home/ubuntu/blockchain-explorer/app/FabricClientProxy.js:32:8)
As the error suggests, you have specified a directory instead of a file on which read operation is happening, I think somewhere in the config.json file you have given an incorrect path. Please check or post the config.json file so the issue can be identified.

error: uncaughtException: require(...).invokeRolesPolicies is not a function

I am using MEAN.JS for developing a web app, but for some reason after I made few changes, well mostly copied and paste new modules into the app I see the error as below:
error: uncaughtException: require(...).invokeRolesPolicies is not a function
I dont know why is this happening, anyone has any clue how to fix this?
This is the whole error message:
TypeError: require(...).invokeRolesPolicies is not a function
at C:\oferdo\oferdo\config\lib\express.js:180:39
at Array.forEach (native)
at Object.module.exports.initModulesServerPolicies (C:\oferdo\oferdo\config\lib\express.js:179:32)
at Object.module.exports.init (C:\oferdo\oferdo\config\lib\express.js:252:8)
at C:\oferdo\oferdo\config\lib\app.js:29:23
at C:\oferdo\oferdo\config\lib\mongoose.js:36:15
at C:\oferdo\oferdo\node_modules\mongoose\lib\connection.js:284:19
at open (C:\oferdo\oferdo\node_modules\mongoose\lib\connection.js:511:17)
at C:\oferdo\oferdo\node_modules\mongoose\lib\connection.js:518:7
at C:\oferdo\oferdo\node_modules\mongoose\node_modules\mongodb\lib\db.js:1504:5
at handleCallback (C:\oferdo\oferdo\node_modules\mongoose\node_modules\mongodb\lib\utils.js:96:12)
at _callback (C:\oferdo\oferdo\node_modules\mongoose\node_modules\mongodb\lib\db.js:1420:5)
at C:\oferdo\oferdo\node_modules\mongoose\node_modules\mongodb\lib\db.js:1463:7
at C:\oferdo\oferdo\node_modules\mongoose\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:1416:5
at finish (C:\oferdo\oferdo\node_modules\mongoose\node_modules\mongodb\node_modules\mongodb-core\lib\auth\scram.js:157:16)
at handleEnd (C:\oferdo\oferdo\node_modules\mongoose\node_modules\mongodb\node_modules\mongodb-core\lib\auth\scram.js:170:7)
at C:\oferdo\oferdo\node_modules\mongoose\node_modules\mongodb\node_modules\mongodb-core\lib\auth\scram.js:264:17
at commandCallback (C:\oferdo\oferdo\node_modules\mongoose\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:1194:9)
at Callbacks.emit (C:\oferdo\oferdo\node_modules\mongoose\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:119:3)
at null.messageHandler (C:\oferdo\oferdo\node_modules\mongoose\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:358:23)
at Socket. (C:\oferdo\oferdo\node_modules\mongoose\node_modules\mongodb\node_modules\mongodb-core\lib\connection\connection.js:292:22)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:153:18)
at Socket.Readable.push (_stream_readable.js:111:10)
at TCP.onread (net.js:531:20)
Actually, this is nothing to do with incorrectly named files.
You need to make sure that you have the invokeRolesPolicies function defined within your policies file. The minimum required is:
'use strict';
var acl = require('acl');
acl = new acl(new acl.memoryBackend());
exports.invokeRolesPolicies = function () {};
The reason why is that the express.js lib defined in mean.js assumes this is set, and tries to call it, hence the error.
You'll get this error if you have an incorrectly named file under your module's server > policies directory, so check there for any inconsistently named files.
EDIT
I see now that it's not the naming of the file but if you have a file in the policies directory that doesn't contain this method, it will produce this error. In my case I had temporarily commented out the code in a file in this directory in preparation for removal.

Error EMFILE, too many open files in ejs

I am using ejs-local to render views for my node.js application. I saw lots of too many open files" exception when rendering views:
Error: EMFILE, too many open files '/home/user/app/views/index.html'
at Object.fs.openSync (fs.js:432:18)
at fs.readFileSync (fs.js:289:15)
at Object.exports.renderFile (/home/wangchen/bweb_auto/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:313:9)
at View.module.exports [as engine] (/home/user/app/node_modules/ejs-locals/index.js:85:7)
at View.render (/home/user/app/node_modules/express/lib/view.js:76:8)
at Function.app.render (/home/user/app/node_modules/express/lib/application.js:506:10)
at ServerResponse.res.render (/home/user/app/node_modules/express/lib/response.js:759:7)
at /home/user/app/routes/index.js:226:17
at Query._callback (/home/user/app/node_modules/orm/lib/ChainFind.js:190:13)
at Query.Sequence.end (//home/user/appode_modules/mysql/lib/protocol/sequences/Sequence.js:75:24)
Which tear down the entire website. Although seemingly adding ulimit might solve this problem: it current is set to 1024. I am more concerned about possible bug in ejs code/node, that didn't close the file when necessary.
Have you come across similar thing?

Error: ENOENT, no such file or directory 'data.json' when running `jsonplaceholder`

When I run jsonplaceholder I get:
jsonplaceholder
JSONPlaceholder listening on http://localhost:3000
fs.js:432
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT, no such file or directory 'data.json'
at Object.fs.openSync (fs.js:432:18)
at Object.fs.readFileSync (fs.js:286:15)
at Object.index.load (/usr/local/lib/node_modules/jsonplaceholder/node_modules/json-server/node_modules/lowdb/node_modules/underscore.db/src/node.js:11:24)
at Function.low.load (/usr/local/lib/node_modules/jsonplaceholder/node_modules/json-server/node_modules/lowdb/lib/index.js:110:29)
at reset (/usr/local/lib/node_modules/jsonplaceholder/index.js:6:14)
at Server.<anonymous> (/usr/local/lib/node_modules/jsonplaceholder/index.js:14:3)
at Server.g (events.js:180:16)
at Server.EventEmitter.emit (events.js:92:17)
at net.js:1052:10
at process._tickCallback (node.js:415:13)
Im on Ubuntu 13.10
Project's author here. Just to let you know, finally, this (old) bug was fixed. Sorry for the inconvenience caused.
I'm still not sure if this has anything to do with an outdated module, or a messed up npm installation or maybe I'm just lucky, but basically the problem is that the path to access "data.json" is wrong (maybe not wrong but incomplete).
What I did was open file:
gksudo gedit /usr/local/lib/node_modules/jsonplaceholder/index.js
and modify the following function:
function reset() {
// server.low.load('data.json') <-- ORIGINAL LINE
server.low.load(require('path').resolve(__dirname, 'data.json')); //<--MY FIX
}
Just save the file, and run jsonplaceholder again.

Resources