node-csv: Error parsing triggers: Cannot find module 'csv-parse/sync'
Describe the bug
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.
To Reproduce
Import 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
Additional context
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.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 12
- Comments: 23 (3 by maintainers)
This is a nightmare
I have a similar issue outside of firebase in a plain TypeScript script using version 5.0.4
As you can see in the csv-parse package.json file the lib/sync is exported
./sync, so you will be able to import lib/sync like this:import { parse } from 'csv-parse/sync';Edit: Jest 28 no longer has this issue: https://github.com/adaltas/node-csv/issues/323#issuecomment-1112595978
Version
6.0.5is also throwing an error with Jest 27 Experimental ESM support + Node v16.11.0 (ESM):package.jsonindex.test.js(import copied from the docs)Running
npm testyields:Workaround for Jest (throws in Node.js):
Throws this error in Node.js:
Workaround for both Jest and Node.js:
Yeah, if you expand the details, you can see the problem.
but that directory doesn’t contain an index.js; it contains an index.cjs. https://unpkg.com/browse/csv-parse@5.4.0/dist/cjs/
@wdavidw maybe this is actually a problem in the published version of
csv-parse(and other packages) - check out the error report below from Are the types wrong? by @andrewbranch(this also happens for
csv-generate, and may affect othernode-csvpackages)Had a related problem compiling with pkg. TS using modules would work using
require("csv/sync")but pkg cannot find the module. Usingrequire("csv/dist/cjs/sync.cjs")works for compiling but TS rejects, complaining it can’t find the module. Finally able to use both TS and pkg for this by usingvar { generate, parse, transform, stringify } = require("../../node_modules/csv/dist/cjs/sync.cjs");That is, using this in a .ts ESM file works for build (for dev/debug) and compiling.This is no longer throwing on Jest 28 🎉 Thanks to support for the
exportsfield inpackage.json.So the version from the docs can now be used with Jest. (although due to https://github.com/import-js/eslint-plugin-import/issues/1810 it is still incompatible with
eslint-plugin-import’sno-unresolvedrule)TS won’t match the declaration file when called with ESM syntax for whatever reason
Me too, with TS and csv-parse. And I don’t want to install whole
csv@6.3.2just to parse a small .csv because of these errors… Made it work with an awkward mix of ESM & CJS:import { parse } from "../../node_modules/csv-parse/dist/cjs/sync.cjs";based on @shunyun-bp answer above.Still have the issue. Eslint is looking for “/node_modules/csv-parse/dist/esm/sync” instead of “/node_modules/csv-parse/lib/sync”. This is weird because the package.json looks correct.
I’m using TypeScript btw. Is it only a TS issue?
Is it just me?
Could you check the latest version 5.4.1 with Are types wrong ?, the “csv-parse/sync” with node16 from CJS is now ok after the changes from #397.
Using
requireand passing the CommonJS path from thedistdir innode_modulesworked instead of the ESM dir with import. I thoughtts-nodewas transforming ESMimport/exportbefore evaluating deps. Thanks for the tip.@bnwa check inside your
node_modulesfolder - I think there is a non-ESM version of the file.Should be in the
node_modules/csv-parse/distfolder, but NOT in theesmfolder - look through the other folders for a CommonJS version