Read Local CSV file into a 2D Array - Javascript - javascript

Hoping I could get some help with reading a csv file into a 2d array for a node app.
I've seen other questions with answers which suggest papaparse, jquery-csv, and csv npm packages. I have read the documentation for each of those solutions and do not see where in the process would my program read the csv.
I have a csv file in my documents folder: C:\Documents\Folder\data.csv for example.
I was hoping I could have the program read the data.csv which is formatted like so
header0 header1 header2 header3 header4 header5 header6 header7 header8 header9
data data data data data data data data data data
data data data data data data data data data data
and place it into an array called myarray
myarray[0][9] would give me header9 and so on.
Here is what I have when I tried jquery-csv
var fs = require('fs');
var csv = require('./jquery.csv.js');
var sample = 'C:\Documents\Folder\data.csv';
var myarray = $.csv.toArrays(sample);
The error I receive is
ReferenceError: $ is not defined
at Object.<anonymous> (C:\Users\0121160\Documents\Nodejs\readcsv.js:7:14)
[90m at Module._compile (internal/modules/cjs/loader.js:959:30)[39m
[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)[39m
[90m at Module.load (internal/modules/cjs/loader.js:815:32)[39m
[90m at Function.Module._load (internal/modules/cjs/loader.js:727:14)[39m
[90m at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)[39m
[90m at internal/main/run_main_module.js:17:11[39m
note: I have very minimal experience with javascript, node, jquery.
Any help would be greatly appreciated.
Thanks!

From https://www.npmjs.com/package/jquery ...
To include jQuery in Node, first install with npm.
npm install jquery
For jQuery to work in Node, a window with a document is required. Since no such window exists natively in Node, one can be mocked by tools such as jsdom. This can be useful for testing purposes.
require("jsdom").env("", function(err, window) {
if (err) {
console.error(err);
return;
}
var $ = require("jquery")(window);
});

The error says $ is not defined, so you need to use var myarray = csv.toArrays(sample);.
The variable $ does not mean anything to your program as you haven't imported anything that's assigned to the variable $.
I also doubt var csv = require('./jquery.csv.js'); will work, you'll probably have to npm install jquery-csv and then import var csv = require('jquery-csv') into your module.

jQuery is designed for helping do common things with JavaScript embedded on a webpage. jQuery CSV is a plugin for it.
Since you are writing software to run on Node.js and not in a webpage, jQuery isn't a great choice of library… so don't use it or a plugin for it.
Get a CSV parser optimised for Node.js such as csv-parser:
const fs = require('fs');
const csv = require('csv-parser')
const path = 'C:/Documents/Folder/data.csv';
const results = [];
fs.createReadStream(path)
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
console.log(results);
});

Related

Best way to use recent D3 versions to produce PNGs from node.js?

I'm wanting to use d3 server side to create PNG files, directly or via SVG.
I can find a number of discussions here about how to achieve this, but they're all from many years ago, referring to d3 v3 or v4, and node back to v10.
Is it still necessary to pretend we're in a browser, providing a pseudo-DOM using something like jsdom?
Does the alternative d3-node family of packages actually still work? They're mostly not had updates for some years.
Here's my MNWE on node 17.0.1, d3 7.1.1, d3-node 2.2.3 on macOS 11.6:
const fs = require('fs');
const d3 = require('d3-node')().d3;
const output = require('d3node-output');
const d3nBar = require('d3node-barchart');
// const d3nPie = require('d3node-piechart');
// const csvString = fs.readFileSync('mentions.csv').toString();
const csvString=`key,value
Bob,33
Robin,12
...
Stacy,20
Charles,13
Mary,29`
const csvData = d3.csvParse(csvString);
const selector = `#chart`
const container = `<div id="container"><h2>Bar Chart</h2><div id="chart"></div></div>`
const style = `.bar{fill: steelblue;}
.bar:hover{fill: brown;}
.axis{font: 10px sans-serif;}
.axis path,.axis line{fill: none;stroke: #000;shape-rendering: crispEdges;}
.x.axis path{display: none;}`
// create output files
// const pie = d3nPie(csvData, selector, container, style)
const bar = d3nBar(csvData, selector, container, style)
output('output', bar);
I get the following errors in the penultimate line arising in the d3node-barchart library:
/Users/j/Dropbox/NP-other-tests/node_modules/d3node-barchart/index.js:55
x.domain(data.map((d) => d.key));
^
TypeError: Cannot read properties of undefined (reading 'map')
at bar (/Users/j/Dropbox/NP-other-tests/node_modules/d3node-barchart/index.js:55:17)
at Object.<anonymous> (/Users/j/Dropbox/NP-other-tests/d3test.cjs:45:13)
at Module._compile (node:internal/modules/cjs/loader:1095:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47
Investigation
The error message tells that d3n-barchart does not find data where it expects it to be.
Looking at the file throwing the error: node_modules/d3node-barchart/index.js, it appears that function bar expects one object as parameter, and not a data array as first parameter.
function bar({
data,
selector: _selector = '#chart',
...
It seems that the syntax shown in the README of d3n-bar is incorrect.
As shown in the example file of the library, function d3nBar takes one object as input parameter, with a data property.
Solution
Applying the syntax from d3n-barchart's example file solves the error:
const bar = d3nBar({
data: csvData
, selector: selector
, container: container
, style: style
})
You might want to correct d3n-barchart's README file and submit a pull request, to help future users get their code right :)

How to use node_modules in Deno as typescript imports?

Project: REST API for serving information stored in a neo4j graph database.
Backend: Deno
I am farely new to deno, but I'm not new to typescript, having used it in Angular frequently.
Problem: I want to use a driver to connect my neo4j database to my backend, but there is no neo4j driver made for Deno. I have scoured the internet and documentation for solutions, and have been trying to import the javascript library using the node modules import tool that has been suggested from similar answers and is supported by the deno team.
Essentially, I do npm install neo4j-driver, and then add the following code to my deno project.
Failed Solution: the javascript node modules wrapper
I implement call this function as a test for my deno server in a server.ts file.
The command I use for deno is: deno run --allow-all --unstable server.ts
neo4j_conn.ts file: (called by server.ts)
import { createRequire } from "https://deno.land/std/node/module.ts";
const require = createRequire(import.meta.url);
export async function testconnection(uri: string, user: string, password: string) {
//This is the line that fails.
var neo4j = require('neo4j-driver').v1; //this fails whether or not I include the .v1 or not.
var driver = neo4j.driver(uri, neo4j.auth.basic(user, password))
const session = driver.session()
const personName = 'Alice'
try {
const result = await session.run(
'CREATE (a:Person {name: $name}) RETURN a',
{ name: personName }
)
const singleRecord = result.records[0]
const node = singleRecord.get(0)
console.log(node.properties.name)
} finally {
await session.close()
}
await driver.close()
}
This returns the following error:
error: Uncaught (in promise) Error: Cannot find module 'net'
Require stack:
- /mnt/c/Users/xxxxx/source/private_logic/deno-try/node_modules/neo4j-driver-bolt-connection/lib/channel/node/node-channel.js
- /mnt/c/Users/xxxxx/source/private_logic/deno-try/node_modules/neo4j-driver-bolt-connection/lib/channel/node/index.js
- /mnt/c/Users/xxxxx/source/private_logic/deno-try/node_modules/neo4j-driver-bolt-connection/lib/channel/index.js
- /mnt/c/Users/xxxxx/source/private_logic/deno-try/node_modules/neo4j-driver-bolt-connection/lib/bolt/handshake.js
- /mnt/c/Users/xxxxx/source/private_logic/deno-try/node_modules/neo4j-driver-bolt-connection/lib/bolt/index.js
- /mnt/c/Users/xxxxx/source/private_logic/deno-try/node_modules/neo4j-driver-bolt-connection/lib/index.js
- /mnt/c/Users/xxxxx/source/private_logic/deno-try/node_modules/neo4j-driver/lib/index.js
- /mnt/c/Users/xxxxx/source/private_logic/deno-try/neo4jconn.ts
at Function._resolveFilename (https://deno.land/std#0.97.0/node/module.ts:273:19)
at Function._load (https://deno.land/std#0.97.0/node/module.ts:380:29)
at Module.require (https://deno.land/std#0.97.0/node/module.ts:133:21)
at require (https://deno.land/std#0.97.0/node/module.ts:1158:16)
at Object.<anonymous> (file:///mnt/c/Users/xxxxx/source/private_logic/deno-try/node_modules/neo4j-driver-bolt-connection/lib/channel/node/node-channel.js:24:29)
at Module._compile (https://deno.land/std#0.97.0/node/module.ts:168:36)
at Object.Module._extensions..js (https://deno.land/std#0.97.0/node/module.ts:1109:10)
at Module.load (https://deno.land/std#0.97.0/node/module.ts:147:34)
at Function._load (https://deno.land/std#0.97.0/node/module.ts:413:14)
at Module.require (https://deno.land/std#0.97.0/node/module.ts:133:21)
As far as I could tell, I had done everything right, but I am a little in over my head when it comes to the typescript/js module translation.
My file structure is as follows:
package.json
package-lock.json
server.ts
neo4j_conn.ts
node_modules -|
|
:
Neo4j developer js docs: https://neo4j.com/developer/javascript/
Deno node modules "require": https://doc.deno.land/https/deno.land/std#0.97.0/node/module.ts
If you look at the Node compatibility layer README in std you will realize that right now there is no compatibility module for the net library. The compatibility will improve day by day, but take into account that Deno is not a drop in replacement for Node, but a whole new thing that won't work with Node libraries by default
https://deno.land/std#0.97.0/node

Undefined variable, nodejs, json to program variable

I decided to save data the dirty way to a .json file. For some reason, when I run my index.js file which runs other modules I have written, it says that a particular variable I initialized in a separate module is undefined (one I was hoping to reference from json). The structure of my program is the standard index file that loads functions from modules I have written and executes them via endpoints.
.json File
{"blocks":[{"GENESIS_DATA":{"timestamp":1,"lastHash":"v01d","hash":"?M=(((Position-1)=>ter)=>sen)=>non?","difficulty":20,"nonce":0,"data":[]}}]}
I want to take the first index of this array named GENESIS_DATA and use it as an array in my program...
relevant code from blockchain index (not the file I execute for the program to run)
const { REWARD_INPUT, MINING_REWARD, GENESIS_DATA } = require('../config');
const fs = require('fs');
const jsonRoute = '/home/main/public_html/Cypher-Network/CDSM/CDSM.json';
class Blockchain {
constructor() {
fs.readFile(jsonRoute, 'utf-8', function(err, data) {
if (err) throw err;
this.jsonChain = JSON.parse(data);
const genesis = jsonChain.blocks[0];
});
this.chain = [genesis];
}
/*Alot more code down here but let's assume that the bracket for class Blockchain is completed*/
}
error log
/home/main/public_html/Cypher-Network/blockchain/index.js:32
this.chain = [genesis]; //we are taking the first element of the json file (genesis block)
^
ReferenceError: genesis is not defined
at new Blockchain (/home/main/public_html/Cypher-Network/blockchain/index.js:32:19)
at Object.<anonymous> (/home/main/public_html/Cypher-Network/index.js:28:20)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47
main#goldengates.club [~/public_html/Cypher-Network]#
First, the constant genesis is local to the callback, so it is getting destroyed just after the callback has finished running.
Also even if the constant was declared outside the callback, remember that fs.readFile is asynchronous, so while readFile is reading the file containing the data, the constant genesis will have already been set to undefined.

generating spectrogram form wav file nodejs and save it

i'm new in the world of javascript and i wanted to recreate a project of mine (written in python) with javascript.
so i want to write a script with nodejs that reads a wav file and generate from it a spectrogram.
I used node-wav , canvas and audio-context trying to recreate the method applied in this module
I'm working with local files from my laptop and with javascript client side. I want to do everything locally. This is my code :
const canvas = createCanvas(480, 240)
const spectro = spectrogram(canvas, false)
const spectrogramsGenerator = async () => {
// wavfiles return a list of wav files (it works)
const files = await wavFiles()
for (file in files) {
const buffer = fs.readFileSync('path/to/file/' + files[file])
spectro.connectSource(buffer, audioContext)
spectro.start()
}
}
with that code snippet i get the following error :
/path/to/workspace/node_modules/spectrogram/spectrogram.js:34
window.onresize = function() {
^
ReferenceError: window is not defined
at new Spectrogram (/path/to/workspace/spectrogram/spectrogram.js:34:5)
at Spectrogram (/path/to/workspace/node_modules/spectrogram/spectrogram.js:16:14)
at Object.<anonymous> (/path/to/workspace/spectrogram.js:13:17)
at Module._compile (internal/modules/cjs/loader.js:736:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:747:10)
at Module.load (internal/modules/cjs/loader.js:628:32)
at tryModuleLoad (internal/modules/cjs/loader.js:568:12)
at Function.Module._load (internal/modules/cjs/loader.js:560:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:801:12)
at executeUserCode (internal/bootstrap/node.js:526:15)
if this the wrong way doing it (i think it is) .. what packages should i use to achieve my goal or how should i approach this problem. Thanks in advance

NodeJs : TypeError: require(...) is not a function

I am trying to require a file and afterwards pass it to a var. I am following this tutorial to create an authentication system. After writing the server.js file and trying to compile I got a BSON error therefore I changed the line that required the release version of it in mongoose.
Here are my code and error:
server.js
require('./app/routes')(app, passport);
Error
require('./app/routes')(app, passport);
^
TypeError: require(...) is not a function
at Object.<anonymous> (d:\Node JS learning\WorkWarV2\server.js:38:24)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
at node.js:951:3
Process finished with exit code 1
I have read that this usually means that requireJS is not getting loaded properly yet I am not aware why or how to fix it.
Edit due to comment:
As asked, here is the result of console.log(require);
For me, when I do Immediately invoked function, I need to put ; at the end of require().
Error:
const fs = require('fs')
(() => {
console.log('wow')
})()
Good:
const fs = require('fs');
(() => {
console.log('wow')
})()
I think this means that module.exports in your ./app/routes module is not assigned to be a function so therefore require('./app/routes') does not resolve to a function so therefore, you cannot call it as a function like this require('./app/routes')(app, passport).
Show us ./app/routes if you want us to comment further on that.
It should look something like this;
module.exports = function(app, passport) {
// code here
}
You are exporting a function that can then be called like require('./app/routes')(app, passport).
One other reason a similar error could occur is if you have a circular module dependency where module A is trying to require(B) and module B is trying to require(A). When this happens, it will be detected by the require() sub-system and one of them will come back as null and thus trying to call that as a function will not work. The fix in that case is to remove the circular dependency, usually by breaking common code into a third module that both can separately load though the specifics of fixing a circular dependency are unique for each situation.
For me, this was an issue with cyclic dependencies.
IOW, module A required module B, and module B required module A.
So in module B, require('./A') is an empty object rather than a function.
How to deal with cyclic dependencies in Node.js
Remember to export your routes.js.
In routes.js, write your routes and all your code in this function module:
exports = function(app, passport) {
/* write here your code */
}
For me, I got similar error when switched between branches - one used newer ("typescriptish") version of #google-cloud/datastore packages which returns object with Datastore constructor as one of properties of exported object and I switched to other branch for a task, an older datastore version was used there, which exports Datastore constructor "directly" as module.exports value. I got the error because node_modules still had newer modules used by branch I switched from.
I've faced to something like this too.
in your routes file , export the function as an object like this :
module.exports = {
hbd: handlebar
}
and in your app file , you can have access to the function by .hbd
and there is no ptoblem ....!
I don't know how but in may case it got fixed when I changed
require('./routes')(app)
to
require('./routes')
In my case i fix when i put the S in the module.exportS,
BEFORE:
module.export = () => {}
AFTER:
module.exports = () => {}

Categories

Resources