Node js - Write data to CSV File - javascript

I am trying to write incoming data to a csv file.
I tried to use http://csv.adaltas.com/generate/examples/
npm install csv
But I don't get the documentation. I just want to save some data into a csv file. But even the examples are not working.
E.g. the example in the link above (Using the stream API) throws the following error message:
TypeError: Cannot read property 'eql' of undefined
(First I had to change the require path, after installing the csv full package I had to require it with:
var generate = require('csv/node_modules/csv-generate');
Obviously this guy uses the same node module:
https://masteringmean.com/lessons/18-Writing-to-a-CSV-file-in-Nodejs
But I can't even get his code to work. I think it was made with an older (and better documented?!) version of the csv node module.
Can anyone help me? Maybe with some code, explaining the same basic things like the one seen in the link of masteringmean.com but for the newest version?
Thanks for any hints.
Cheers

should.eql() statement that triggers the error is an assertion made with Should. You have install it at first:
npm install should --save
And require it:
var should = require('should');
The error should disappear.

better yet, just remove these lines:
output.should.eql('OMH,ONKCHhJmjadoA\nD,GeACHiN');
and/or
data.should.eql([ [ 'OMH', 'ONKCHhJmjadoA' ],[ 'D', 'GeACHiN' ] ]);
you really don't want the overhead of a library you're not using.

Related

Uncaught TypeError: Cannot read properties of undefined (reading 'json_to_sheet')

I am getting this error and do not know what to do. I have not used node_modules before, so it would be nice with help.
my files:
i am using the test.js filecode:
const XLSX = fetch('xlsx')
// array of objects to save in Excel
let binary_univers = [{'name': 'Hi','value':1},{'name':'Bye','value':0}]
console.log(binary_univers)
let binaryWS = XLSX.utils.json_to_sheet(binary_univers);
// Create a new Workbook
var wb = XLSX.utils.book_new()
// Name your sheet
XLSX.utils.book_append_sheet(wb, binaryWS, 'Binary values')
// export your excel
XLSX.writeFile(wb, 'Binaire.xlsx');
What i am trying to do is to have data into xlsx, but for some reason it does not know the function "json_to_sheet".
please comment if you know how to fix it! I am also unsure on what Node.js is, so if that is related please explain to me.
Discord:
Abinesh🦞#6158
UPDATE:
I have done what you said, but i wont still work. DId i do something wrong? (look at the picture)
(i am using the test.js file)
you need to install xlss first to your project, open a terminal and go to you project path and install the xlss liberary
i.e
$ npm install xlsx
other ways to install
once installed, you can use this without any error
const xlsx = require('xlsx');
updated answer:
after looking into your package.json , it's clear it's not configured properly, so follow this stpes
Create an empty folder and open it in terminal.
type npm init -y on the empty folder, once done you should see a proper package.json
type npm install xlsx
create a new file and name is whatever you like (e.g myFile.js)
and paste you excel code there.
make an edit in package.json on the script key, like this
"scripts": {
"start": "myFile.js"
},
Now you run the code and should not see any error, command to run node .\myFile.js

Json "require" converted to Module "require" when installing packages

So... Lets say you have this library you want to post to NPM.
You convert it to a module type and use:
import {createRequire} from "module";
const require = createRequire(import.meta.url);
To implement require onto it. As you may know require can be used to read JSONs. So you try to read a json. Everything works fine when you test it onto a local machine. When you try linking it tho and you try to call that function it throws:
Error: Cannot find module 'something.json'
Is this an on going npm issue and if so how can i avoid it? Is there another way to read JSONs other than reading them as a txt file (which isn't very practical)?

require is not defined at npm.js:2

Forgive me if this is something simple as my research has come up empty in regards to this specific issue. I am using Bootstrap the most recent release. I have noticed that there is a file that is new from previous releases called npm.js. I am hosting all the Bootstrap files locally on my hosted web server. I call the file like all the others in the head to the full path to the file. In the console I have this error.
npm.js:2 Uncaught ReferenceError: require is not defined
at npm.js:2
Which is referring to this line.
require('../../js/transition.js')`
Which is from this npm.js file that is in the newest Bootstrap
// This file is autogenerated via the `commonjs` Grunt task. You can require() this file in a CommonJS environment.
require('../../js/transition.js')
require('../../js/alert.js')
require('../../js/button.js')
require('../../js/carousel.js')
require('../../js/collapse.js')
require('../../js/dropdown.js')
require('../../js/modal.js')
require('../../js/tooltip.js')
require('../../js/popover.js')
require('../../js/scrollspy.js')
require('../../js/tab.js')
require('../../js/affix.js')
I am unfamilar with this so I am not sure why I am getting this error but, how can I surpress this error and fix this issue? Do I even need this file? From what I understand if I am using modals or tooltips etc I need this. Is this correct?
You will need to download/config the missing libraries or requires. I am not sure which libraries you are using.
You can add the missing libraries to your package.json. Then call for npm install.

How can I use jStorage (or other external JS libs) with Ember.js via Ember-CLI

I had a small Ember-App in just one single HTML File and everything was working fine, but since it was getting quite big I started to port it to Ember-CLI. Most things worked fine to port but I'm still struggling to add JStorage:
https://github.com/andris9/jStorage
I'm not really sure how to start as its a plain JS Lib, that I normally would just drop into the code somewhere before I use it. Now with all the modules I'm totally lost where to even start looking for how to do it.
Can anyone point me in the right direction how to use such JS Libs?
I found a few Topics around it but did not get to any working path.
Here is how I used it previously:
App.Something = Ember.Object.extend({
init: function() {
var stored = $.jStorage.get('something');
...
}
});
Well, after a lot of smoke out of my ears i got it to work:
add json2
$ bower install --save json2
This does not work out of box because there is no tags in the repo.
Edit the bower.js File to set the version to "master". Then it works.
add jStorage
$ bower install --save jstorage
Install the dependencies (not sure if necessary but i did it)
$ ember install:bower
Then the files are available in the folder bower_components, which is ignored by git and apparently my editor (atom.io) too.
Import the files in the Brockfile.js like this
...
app.import('bower_components/json2/json2.js');
app.import('bower_components/jstorage/jstorage.js');
module.exports = app.toTree();
Use it, prefixing $ with Ember not to upset JSHint (would work without)
...
var stored = Ember.$.jStorage.get(id);
...

What is this error that comes up in node.js?

I was trying to follow this tutorial.
Then when I got to this part
node jsctags/bin/jsctags --sort=yes --locals tst.js
I got the following warning message.
The "sys" module is now called "util". It should have a similar
interface.
I'm doing this in OSX. First I tried the package and then I tried installing from source. I still get the same message.
What does this mean? What can I do about it?
In your jsctags file you probably have a line that looks like this:
sys = require ('sys');
As a first step, try using this line:
sys = require ('util');
This will still refer to the package by the name sys in your script, so the rest should work; but in the future, util = require ('util'); might be better, to make it more clear that you're using the newer package and API.
The message is just Node's way of telling your sys module is deprecated and everyone should migrate to util.
The reason you're getting it may not be your fault. If any of the libraries you're using not migrated to the new modules, it'll also show that message.

Categories

Resources