We want to do this, but with Typescript:
const papa = require('papaparse');
const fs = require('fs');
const file = fs.readFileSync('sample.csv', 'utf8');
papa.parse(file, {
complete: (result) => console.dir(result.data)
});
Approach
Install ts-node in order to execute our .ts
files directly without having to compile them:
npm i -g ts-node
You may also wish to create an alias such that you can just type ts each time you want to run your script.
npm install --save @types/node
The readFileSync function can now be imported like this:
import {readFileSync} from 'fs';
Install the types for Papaparse:
npm i -g @types/papaparse;
Import the parse function directly like this:
import { parse } from ‘papaparse’;
The entire equivalent typescript final version is:
import { parse } from 'papaparse';
import { readFileSync } from 'fs';
const file = readFileSync('sample.csv', 'utf8');
parse(file, {complete: (result) => console.dir(result.data)});
Now you can go CSV crazy! Just make sure you keep all fingers over the laptop keyboard at all times. Enjoy.