I am new to svelte, sapper. I am trying to read a csv file located at src/_data.csv
from a .js
file in routes. and it gives error of no such file or directory
. Can someone help to find the a way to implement it. I am trying in existing template with webpack as build-tool. Here is the structure
In _post.js
I am trying to read a file present at the root src/_data.csv
.
const csv = require('csvtojson');
const path = require("path");
const csvFilePath=path.join(__dirname, '_data.csv');
//console.log("csvFilePath", csvFilePath);
csv()
.fromFile(csvFilePath)
.then(function(data){
console.log("json data", data)
})
My aim is to read a .csv file and convert it to JSON
, possibly on every request. I know that webpack handles __dirname
to /
as well and there is no .csv
file in __sapper__
i.e the build. But not sure how to make it work in sapper.
Since Rollup doesn't handle
__dirname
, you probably want to put the CSV file in a known directory outsidesrc
(e.g.data/data.csv
) and read it that way:As long as the
data
directory is deployed with the rest of your app, and you start the app with the correctprocess.cwd()
, this will work. (If you're just deploying the contents of__sapper__
then you may need to copy it into the deployed directory, but the basic principle is the same.)