Node.js - Function not working when exported - node.js

Good day everyone,
First off, thanks for always being such an amazing community. You all really are helping me a ton with learning and bettering my programming and development!
I have a small question related to the module.exports within Node.js. The function below runs with no issues when called on directly:
const fs = require('fs')
const {nanoid} = require('nanoid')
const createStormDB = () => {
return new Promise((resolve, reject) => {
try{
const id = nanoid(4)
const date = new Date() // Create date string for file naming
let dateString = `${date.toISOString().split('T')[0]}` // Create date string for file naming
let fileName = `${dateString}_deals_${id}.stormdb` // Create date string for file naming
fs.openSync(`../StormDB/${fileName}`, 'w')
resolve(fileName)
}catch(err){
reject(err)
}
})
}
module.exports = createStormDB
It creates a file with a specific name within in specific folder. But when I use module.exports = createStormDB I am greeted with the following error:
(node:12516) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open '../StormDB/2021-07-19_deals_gYmJ.stormdb'
at Object.openSync (fs.js:476:3)
at C:\Node\Pipedrive-Connector\PipeDriveRequest\scripts\createStormDBFile.js:11:16
at new Promise (<anonymous>)
at createStormDB (C:\Node\Pipedrive-Connector\PipeDriveRequest\scripts\createStormDBFile.js:5:12)
at Object.<anonymous> (C:\Node\Pipedrive-Connector\PipeDriveRequest\play.js:7:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Is there something I am misunderstanding when it comes to exporting modules? I'm importing this module using the require option! Thanks so much for the help!

The .. relative path in the function is relative to the calling scripts current working directory, not the directory the file is in.
Assuming from your path setup and description the database is in: C:\Node\Pipedrive-Connector\PipeDriveRequest\StormDB
If you want the database path to remain relative to the javascript file containing the function, use __dirname
const path = require('path')
const db_path = path.join(__dirname, '..', 'StormDB', filename)
fs.openSync(db_path, 'w')

Related

Read file and write file JSON

In this, I am trying to make a hit counter where every time someone visits my site the variable will be read from the views.json file one is added to the number and then the .json will be updated with the new number. However when I tested it in a repl.it project I got an error saying
ReferenceError: writeFileSync is not defined
at /home/runner/hit-counter/index.js:6:1
at Script.runInContext (vm.js:133:20)
at Object.<anonymous> (/run_dir/interp.js:156:20)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
I don't know what this means if you know please tell me and how I may be able to fix it.
the reply project link:https://hit-counter.cohense.repl.run/
The JavaScript (ES6)
const fs = require('fs');
let views = fs.readFileSync('views.json');
views = JSON.parse(views);
views.total++;
let data = JSON.stringify(views, null, 2);
writeFileSync("views.json", data, finished);
function finished(err) {
if (views = JSON.parse(views)) {
console.log("Your view has been accounted for!")
} else {
console.error("Error occured please reload the page =(")
}
};
the JSON
{
"totalViews": 1
}
You can do like this, just fixed some errors.
Oh, you should use writeFileSync, to avoid that the file will not be edited at same time.
The question is, why don't you use a DB? It's a lot faster and fix concurrency writes.
var fs = require('fs')
var data = fs.readFileSync('views.json')
var views = JSON.parse(data);
console.log(views);
views.total = views.total + 1;
var data = JSON.stringify(views, null, 2)
writeFileSync("views.json", data, ()=>{
console.log("Your View Has Been Accounted For!")
})
I found out what I did wrong I didn't use fs.
writeFileSync("views.json", data, finished);
When I just needed to do
fs.writeFileSync("views.json", data[,finished]);

Error:no such file when using fs.outputJsonSync

I have a problem like this. I have created a compile.js file to compile my solidity Contract. In there to handle file I am using the fs-extra module.
This is my compile.js file
const path = require('path');
const solc = require('solc');
const fs = require('fs-extra');
const buildPath = path.resolve(__dirname,'build');
fs.removeSync(buildPath);
const campaignPath= path.resolve(__dirname,'contracts','Campaign.sol');
const source = fs.readFileSync(campaignPath,'utf8');
const output= solc.compile(source,1).contracts;
fs.ensureDirSync(buildPath);
for(let contract in output){
fs.outputJsonSync(
path.resolve(buildPath,contract),
output[contract]
);
}
It works everything finely, without for each loop. When I compile this file with for loop it gives me an error saying that
C:\Users\tharindusa\WebstormProjects\Campaign\node_modules\solc\soljson.js:1
(function (exports, require, module, __filename, __dirname) { var Module;if(!Modu
le)Module=(typeof Module!=="undefined"?Module:null)||{};var moduleOverrides={};fo
r(var key in Module){if(Module.hasOwnProperty(key)){moduleOverrides[key]=Module[k
ey]}}var ENVIRONMENT_IS_WEB=typeof window==="object";var ENVIRONMENT_IS_WORKER=ty
peof importScripts==="function";var ENVIRONMENT_IS_NODE=typeof process==="object"
&&typeof require==="function"&&!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_WORKER;var EN
VIRONMENT_IS_SHELL=!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_NODE&&!ENVIRONMENT_IS_WOR
KER;if(ENVIRONMENT_IS_NODE){if(!Module["print"])Module["print"]=function print(x)
{process["stdout"].write(x+"\n")};if(!Module["printErr"])Module["printErr"]=funct
ion printErr(x){process["stderr"].write(x+"\n")};var nodeFS=require("fs");var nod
ePath=require("path");Module["read"]=function read(filename,binary){filename=node
Path["normalize"](filename);var ret=nodeFS["readFileSy
Error: ENOENT: no such file or directory, open 'C:\Users\tharindusa\WebstormProje
cts\Campaign\ethereum\build\:Campaign.json'
at Object.fs.openSync (fs.js:646:18)
at Object.fs.writeFileSync (fs.js:1299:33)
at Object.writeFileSync (C:\Users\tharindusa\WebstormProjects\Campaign\node_m
odules\fs-extra\node_modules\jsonfile\index.js:117:13)
at Object.outputJsonSync (C:\Users\tharindusa\WebstormProjects\Campaign\node_
modules\fs-extra\lib\json\output-json-sync.js:15:12)
at Object.<anonymous> (C:\Users\tharindusa\WebstormProjects\Campaign\ethereum\compile.js:15:8)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
Can someone help me to solve this problem? .Thank you
Your filename (:Campaign.json) contains a colon, which is invalid in Windows, where colons are only used with drive letters.
I had run into the same problem while compiling my contracts.The issue with your code is that it tries to create a file named :Campaign which is not allowed in windows.
I used the following code to get around it
for(let contract in output){
fs.outputJSONSync(
path.resolve(buildPath,contract.replace(/:/g,'')+'.json'),
output[contract]
);
}

In Node, how do I elegantly handle requiring modules that read files in their directories?

For instance, in my project directory, I have:
|--bar.js
|--dir
|--foo.txt
|--readfile.js
readfile.js:
const fs = require('fs');
var foo = fs.readFileSync('foo.txt', 'utf8');
console.log(foo);
module.exports = {foo};
Running node readfile.js, everything works perfectly.
bar.js:
const readfile = require('./dir/readfile');
console.log(read.foo);
Running node bar.js, I get:
fs.js:663
return binding.open(pathModule.toNamespacedPath(path),
^
Error: ENOENT: no such file or directory, open 'foo.txt'
at Object.fs.openSync (fs.js:663:18)
at Object.fs.readFileSync (fs.js:568:33)
at Object.<anonymous> (/Users/fterh/Documents/Projects/playground/dir/readfile.js:3:14)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Module.require (module.js:604:17)
at require (internal/module.js:11:18)
Fabians-MacBook-Pro:playground fterh$
I know it has to do with require('./dir/readfile') in bar.js, because Node then tries to search for "foo.txt" in the same directory as "bar.js". Currently, my fix is to use path.dirname(__filename) to get absolute paths, which would work regardless of whether I'm running the module directory or requiring it. I'm wondering if there is a more elegant way of doing things.
Use of require.resolve within readfile.js as follows:
const fs = require('fs');
let foo = fs.readFileSync(require.resolve('./foo.txt'), 'utf8');
console.log(foo);
module.exports = {foo};
Note: in the original question for bar.js it may have been intended to write: console.log(readfile.foo);.
require.resolve:
... return the resolved filename
Use __dirname to construct your path as that will always point to the directory where your module was loaded from, regardless of the current directory. This is one of the variables that is passed into a module so it has a unique value in the scope of each module and it's purpose is for exactly what you want (to do file operations relative to your module's directory).
const fs = require('fs');
const path = require('path');
var foo = fs.readFileSync(path.join(__dirname, 'foo.txt'), 'utf8');
console.log(foo);
module.exports = {foo};
Reference info for __dirname here.

nodejs fs.openSync() with the flags 'a' fails to create a non-exist file

I want to write a log module for my app, which will create a non-exist file according to the time the module is initialized (with the init function in the module is called).
However, when I try to create the new log file with fs.openSync(log_file_name,'a') It always gets an error.
I appreciate anyone that tell me why it fails to create the new file .
const fs=require('fs');
const path=require('path');
const moment=require('moment');
var time=function()
{
return moment().format('YYYY-MM-DD[_]hh:mm:ss:SSS');
};
//init
var fd; // file descriptor
function init(log_dir)
{
var log_file_name=path.join(log_dir,time()+'.log');
this.fd=fs.openSync(log_file_name,'a');
}
init(__dirname);
The error is like below:
"C:\Program Files (x86)\JetBrains\PhpStorm 2016.1\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" index.js
fs.js:634
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open 'I:\twesix\lib\twesix_nodejs\log\2016-05-19_01:34:52:621.log'
at Error (native)
at Object.fs.openSync (fs.js:634:18)
at init (I:\twesix\lib\twesix_nodejs\log\index.js:15:16)
at Object.<anonymous> (I:\twesix\lib\twesix_nodejs\log\index.js:24:1)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Function.Module.runMain (module.js:575:10)
Process finished with exit code 1
I'm using windows 10 and my node.js version is 6.1.0
The problem is that the string returned by moment().format('YYYY-MM-DD[_]hh:mm:ss:SSS'); contains colons (:), which are not allowed in the name of the files on windows.
Change the format() to something that doesn't contain invalid characters for windows file names, like:
return moment().format('YYYY-MM-DD[_]hh-mm-ss-SSS');

'use-strict' enabled but not working in node

I have enabled use-strict mode in my .js file but when I run it, node keeps telling me that I don't have it enabled. PLEASE don't tell me to write "use-strict"; at the top of my file because I already tried that.
Here is my server.js file. I have been trying to see what is wrong but so far stack overflow has not been much help since most people seem to get this working on their first try.
require('use-strict')
'use-strict';
let util = require('util');
let http = require('http');
let Bot = require('#kikinteractive/kik');
var kik_username = process.env.KIK_USERNAME;
var kik_api_key = process.env.KIK_API_KEY;
var kik_baseUrl = process.env.KIK_BASEURL;
// Configure the bot API endpoint, details for your bot
let bot = new Bot({
username: kik_username,
apiKey: kik_api_key,
baseUrl: kik_baseUrl
});
bot.updateBotConfiguration();
bot.onTextMessage((message) => {
message.reply(message.body);
});
// Set up your server and start listening
let server = http.createServer(bot.incoming()).listen(8085);
Everything seems fine but when I run
$ node server.js
I keep getting this error
let util = require('util');
^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
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)
at startup (node.js:148:18)
at node.js:405:3
It tells me to enable strict mode BUT I ALREADY DID THAT. I even required an npm package to make sure I was doing it right! Can anyone make sense of what is happening?
No dash in 'use strict'
'use strict' // not 'use-strict'
Check out the documentation for further reference
You don't need to require an npm package. just put "use strict"; at the top of the js file.

Resources