node karma fs object empty - node.js

I'm trying to run some tests on node using karma. I'm running using both phantom and real browsers.
Whichever way I run I get an error on fs read file functions.
'undefined' is not an object (evaluating 'fs.existsSync')
This is even if I have a very simple file like:
var fs = require('fs');
console.error(fs);
var text = fs.readFile('data.txt', 'utf8');
The first console writes out Object {}. The second one gives me the above error.
I'm assuming that the object is empty.
I'm using the latest version of karma and dependencies.
Can anybody point me in the right direction as to why the fs object is empty/not working.

Karma is the client side js test runner you cannot use node file system on it. To test server side js I suggest to use a different test runner like Mocha. If you use Mocha for the server js namely nodejs you will be able to use node filesystem.
There is another way to read file from the client is using XMLHttpRequest(). Refer you to this question
Javascript - read local text file

Related

Nodejs crashing silently when trying to aceess file system with "fs"

I'm currently working on an Nodejs API server with Express. and on a specific path i had this,
const fs = require("fs/promises");
fs.mkdir(folderPath, {recursive: true})).then(console.log).catch(consolle.log);
"folderPath" is the full path of the folder that i want to create. now when the execution reaches this line the NodeJs server crashes silently without any trace, nothing in the log files, nothing in the try/catch block for the code, nothing in the Express error handler. The request to that specific route will get a 503 error, which is not from express, and i think this error originates from the server it'self after NodeJS crashes.
To make sure that the error is not caused by the parameters passed to fs.mkdir i specifically put the above lines of code into the entry file (index.js) where express app is started, and replaced "folderPath" with an actual path that i'm sure exists.
const fs = require("fs/promises");
fs.mkdir("/home/abcd/efg", {recursive: true})).then(console.log).catch(console.log);
And the NodeJS server crashes as soon as i start it, with out any trace just like i mentioned above.
The next thing i tried is running a "test.js" file with the above lines of code from the terminal as, test.js only contains those two lines of code.
node test.js
test.js is located within the same folder as index.js, and surprisingly it works. the folder get's created without no error, which leaves me confused. Why would it fail when running from index.js, but works fine when running from test.js.
what am i doing wrong here that's causing the crashes, is there a permission thing that i'm not aware of, or is it something else.
here is some detail of the server:
I'm using Node 14.7.0. One other thing is that when the sever crash occurs large size files named "core####" (eg core3424) are created in the api root folder, where index.js is. and also Number of processes and Memory usage Max out.

Make a logger for Node Js

I have a project in Node Js, which executes the project on port 3000 and I access from ngrok with my browser to said localhost port, and it executes a server on port 3001 to make requests to a Maria database db. The project is done in react and the server with express.
I want to save the application logs (errors, warnings, etc.) in a log file so that I can see them whenever I want.
My intention was to use winston, and while I have no problem on the server side (3001), when I try to adapt it to the main project, I get an error that it cannot save files (the reason that appears is that it runs from the browser, and you can't create such a file because you don't have access to the project folders)
Can anyone give me some advice? Am I wrong to use winston, and should I use another?
Greetings and thanks
I've never used winston before and I couldn't find anything online about your error. In the past I've always just used node's fs module to create a log of errors and restarts.
const fs = require('fs')
Node's File System Documentation: https://nodejs.dev/learn/the-nodejs-fs-module
Short YouTube Tutorial: https://www.youtube.com/watch?v=U57kU311-nE

How to get nodejs on client-side on cordova?

I am making a app with cordova. I want the coinmarketcap stats in it. I am using nodejs and it is working fine when I execute it in my command line. However I want to use it in my cordova application. This is the link to the npm package https://www.npmjs.com/package/node-coinmarketcap
When I try to run it in a browser I get the error message require not defined. I believe that error comes because nodesj is a server-side and browser is a client side ? Am I correct ?
My code
var CoinMarketCap = require("node-coinmarketcap");
var coinmarketcap = new CoinMarketCap();
coinmarketcap.multi(coins => {
console.log(coins.getTop(10)); // Prints information about top 10 cryptocurrencies
});
I expect that I can run my code in my browser or cordova application.
You are right, Node.js is a JavaScript run-time environment that executes JavaScript code outside of a browser. You have to find some client-side JS code or execute your node code on your server and send result to your Cordova app.

File sharing using socket.io and nodejs

When I wanted to do file sharing I'm stuck up with this error
The error is fs.creatreadstream() is not a function ? Am i missing out anything even after performing browserify for the client side and using require('fs')
I'm following the below documentation!!!
https://github.com/nkzawa/socket.io-stream

now.js how to get 'now' object without browser (simple client)?

Im playing around with node.js and now.js. Everything works fine. But i would like to make a simple client that i can run from the command-line (so without the browser).
http://nowjs.com/doc/example
In the example a HTML page gets served, and that page includes the now.js file, which creates the magic 'now' object. But on a commmand-line there is no such thing.
For the server i have running (helloworld_server.js)
And the client helloworld_client.js i have:
// client.js
var nowjs = require("now");
// now i need to connect to the server (127.0.0.1:8080)
// so i i need a server object?
server = ????
var everyone = nowjs.initialize(server);
everyone.now.distributeMessage('hi!');
So how do i obtain the 'now' object?
OK, got it. Once you installed now
npm install now
it creates a node_modules folder, inside you see folders for each extension. Deeper you find:
./node_modules/now/examples
and there is the nodeclient_example folder
./node_modules/now/examples/nodeclient_example
its pretty clear from there, but those curious, this is the magic you need:
var nowjs = require('../../lib/nodeclient/now.js');
var now = nowjs.nowInitialize('http://localhost:8080');
and there it it the 'magic' now object
make sure you install :
npm install socket.io-client
otherwise it wasn't working for me!

Resources