Error parsing triggers: Cannot find module 'csv-parse/sync'
Asked Answered
N

4

18

I am using Firebase functions to build an API that parses CSV files.

When I try to use csv-parse/sync instead of csv-parse, my deploy to Firebase Functions fail with the following error:

Error: Error parsing triggers: Cannot find module 'csv-parse/sync''
Require stack:
- /Users/xxx/Programming/xxx/Firebase Functions/xxx/functions/lib/index.js
- /usr/local/lib/node_modules/firebase-tools/lib/deploy/functions/runtimes/node/triggerParser.js

Try running "npm install" in your functions directory before deploying.

I have imported using:

import { parse } from 'csv-parse/sync';

Then use in code like this:

interface EventData {
    update: string;
    id: string;
    title: string;
    description: string;
    category: string;
    ages: string;
    place: string;
    placeCoordinate: string;
    startDate: string;
    startTime: string;
    length: string;
    url: string;
    arrName: string;
  }

let events: Array<EventData> = []
const headers = ["update", "id", "title", "description", "ages", "place", "placeCoordinate", "startDate", "startTime", "length", "url", "arrEpost", "arrName", "validated", "skugg"]
try {
    events = parse(text, {columns: headers, from: 6, quote: "\"", delimiter: ";", ltrim: true, rtrim: true})
}...

I have installed by going to /functions-folder and running

npm install --save csv-parse

Deploying in root folder with

firebase deploy

Is this an issue with the framework, with firebase or am I doing something wrong? Normal use of "csv-parse" without sync works just fine. In both cases it seems to import just fine in Visual Studio Code, but not when deploying with "sync". I have tried to clean the node_modules folder, rebuild the package-lock.json-file, upgraded to the latest version of firebase tools, all without success.

I have added a similar question on the framework project issues page: https://github.com/adaltas/node-csv/issues/323

Nudi answered 23/2, 2022 at 7:43 Comment(2)
Please provide the following information: - package.json engines.node and scripts.build - tsconfig.json target and module - firebase.json functions.predeployForeandafter
Could you tell us which verison of nodejs you're using and what version of csv-parse is installed?Norword
T
11

If you are using Typescript you can do

import  parse  from 'csv-parse/sync';

or

import  {parse}  from 'csv-parse/lib/sync';
Trigon answered 2/3, 2022 at 14:0 Comment(4)
Had a similar problem with importing csv-parse/sync locally in JS and the second option worked for me.Supreme
This is not in the documentation but I think this is the best solution.Trigon
The second option works for me as well (using Typescript), but can anybody explain why it works, and how should I know??Harmonic
Only this worked for me in Bun: import parse from 'csv-parse/lib/sync'Prevenient
B
2

For me either of the two worked in node.js (based on Jeff Paredes answer):

import parse from 'csv-parse/lib/sync';
const parse = require('csv-parse/lib/sync');

It seems like the folder structure within the node_modules folder was changed. So instead of sync.js being located in csv-parse it is now in csv-parse/lib.

enter image description here

Beery answered 29/4, 2022 at 12:47 Comment(0)
M
0

Using [email protected] (and after having tried using csv-parser on its own unsuccesfully), the following is the only combination that worked for me:

import { parse } from "csv-parse/sync";

Example usage:

import * as fs from "fs";
import { parse } from 'csv-parse/sync';

const csvPath = "./data/users.csv";

type UserRow = {
  id: number;
  name: string;
};

const csvContent = fs.readFileSync(csvPath, "utf-8");
const rows: UserRow[] = parse(csvContent, {
  columns: true,
  skip_empty_lines: true,
});

console.log(rows);

I also had to add "moduleResolution": "node" to the compilerOptions of tsconfig.json.

Metry answered 31/8, 2023 at 16:38 Comment(0)
I
-1

As of today, if you install you should just install node-csv by running yarn add csv or pnpm install csv or npm install csv

Then in your code just do

import { parse } from 'csv'

Then you are done

Inner answered 16/1, 2023 at 21:20 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.