This question already has answers here:
Configure Node.js to log to a file instead of the console
(28 answers)
Closed 6 months ago.
i want to write console logs into logs.json file and saves it like:
{
"1":"consolelog1",
"2":"consolelog2",
"3":"consolelog3"
}
Using node.js, you can use fs:
const fs = require('fs');
function log_to_file(mystuff){
// GET file
const file = JSON.parse(fs.readFileSync('./logs.json'));
// Find number of console.logs already there
let lognumb = -1, cont = true;
while(cont){
if(!(file[lognumb+1]===undefined)){
lognumb++;
continue
};
cont=false
};
file[lognumb+1] = "consolelog"+mystuff;
fs.writeFileSync("./logs.json",JSON.stringify(file));
}
Then, you can use log_to_file("your_console_log_data") to save your data.
Make Sure You Are Using NODE.JS, Not JAVASCRIPT
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 days ago.
Improve this question
I'm fetching a JS file over the network.
ie:
// file is a JS file
// for example: console.log("I am file")
const file = await get('/api/file/1')
I'm not sure how to feed this type of data to fs. I think it needs to be a Buffer but I haven't gotten it to work:
const dataAsBuffer = Buffer.from(file, 'utf-8')
Then I try:
fs.writeFileSync(filePath), dataAsBuffer, (err) => { ...
But get: TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined
How can I write a JS file I fetched, to local directory?
EDIT:
the output of get:
// initialize code called once per entity
Test.prototype.initialize = function() {
};
// update code called every frame
Test.prototype.update = function(dt) {
};
console.log('hell oworld');
Fixed: No callback in writeFileSync
This question already has answers here:
using process.env in TypeScript
(19 answers)
Closed 1 year ago.
I'm trying to make a simple api using typescript and when I use any env variable I get an error from TS compiler Tells me that this could be undefined
example
// Not Working
const db = process.env.DB_URL // This gives an error that the result could be a string or undefined
to fix this
I have to make a type guard and check with if statement as follows
const db = process.env.DB_URL
if (db){
// ....
}
Is there a better approach to to such a thing instead of explicitly check for every variable ?
You can apply null check and keep it as string instead of defining two types:
const db: string = process.env.DB_URL ?? ''
// empty strings are falsy/falsey
if (db) { // do this}
else { //do this}
This question already has answers here:
How do I get the path to the current script with Node.js?
(15 answers)
Closed 3 years ago.
If you have a module that's being imported in some code, let's say...
var my_cool_module = require('my_directory/my_cool_module');
my_cool_module.print_directory_name();
And I'm in that module's context, say this is the file my_cool_module.js...
function get_dir_name() {
// get the directory name...
return directory_name;
}
exports.module.print_directory_name = function() {
console.log("This module is in directory " + get_dir_name());
};
How can I get the directory that the module is in (i.e. "my_directory")
I found out from the node.js documentation here that you can use __dirname
function get_directory_name() {
return __dirname;
}
I have a variable named, 'res'. This variable if used with console.log() print all request to a function. My question is... How to log this variable in a log.txt file?
Example using console.log(res)
Print in console:
[ 'AndroidShareANE',
'pixi.js',
'ShortSword',
'faceshiftparser',
'clmutils',
'chaikin-smooth',
'getuservideo',
'getboundingbox',
'clmdraw',
'SpriteSheetScrubberTutorial',
'interpolation-arrays',
This is a one part of response.
My purpose is log in log.txt file the var content. Identical to console.log() result.
Thanks for your collaboration, and sorry my bad english.
The easiest way is to create a new Console object that writes to your file. The docs have an example of this exact thing, which I'll reproduce here for posterity:
const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');
// custom simple logger
const logger = new Console(output, errorOutput);
// use it like console
var count = 5;
logger.log('count: %d', count);
// in stdout.log: count 5
If you don't pass the second argument (errorOutput) to new Console then the error output will also be written to the output file.
This question already has answers here:
node.js require all files in a folder?
(15 answers)
Closed 9 years ago.
Help me please.
I want require all files from directory ?
How best to do it?
I read about require_paths but it does not perform those functions.
You can walk through a directory and require each one:
var mods = {};
var files = fs.readdirSync('your_directory').filter(function(x) { return x.substr(-3) == ".js"; });
for(var i = 0; i != files.length;++i) {
mods[files[i]] = require(path.join('your_directory',files[i]));
}