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)

Most upvoted comments

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';

// ... 
"exports": {
    ".": {
      "import": "./lib/index.js",
      "require": "./dist/cjs/index.cjs",
      "types": "./lib/index.d.ts"
    },
    "./sync": {
      "import": "./lib/sync.js",
      "require": "./dist/cjs/sync.cjs",
      "types": "./lib/sync.d.ts"
    },
    "./browser/esm": {
      "types": "./lib/index.d.ts",
      "default": "./dist/esm/index.js"
    },
    "./browser/esm/sync": {
      "types": "./lib/sync.d.ts",
      "default": "./dist/esm/sync.js"
    }
// ...

Edit: Jest 28 no longer has this issue: https://github.com/adaltas/node-csv/issues/323#issuecomment-1112595978


Version 6.0.5 is also throwing an error with Jest 27 Experimental ESM support + Node v16.11.0 (ESM):

package.json

{
  "type": "module",
  "dependencies": {
    "csv": "6.0.5"
  },
  "scripts": {
    "test": "NODE_OPTIONS=--experimental-vm-modules npx jest"
  },
  "jest": {
    "transform": {},
  }
}

index.test.js (import copied from the docs)

import {generate, parse, transform, stringify} from 'csv/sync';

test('empty test', () => {});

Running npm test yields:

$ /Users/k/projects/project/node_modules/.bin/jest
(node:27879) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
 FAIL  __tests__/index.test.js
  ● Test suite failed to run

    Cannot find module 'csv/sync' from 'index.js'

      at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:324:11)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.144 s, estimated 1 s
Ran all test suites.

Workaround for Jest (throws in Node.js):

import { parse, stringify } from 'csv/dist/esm/sync';

Throws this error in Node.js:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './dist/esm/sync' is not defined by "exports" in /Users/k/projects/changes-codealong/node_modules/csv/package.json imported from /Users/k/projects/changes-codealong/index.js
    at new NodeError (node:internal/errors:371:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:440:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:692:3)
    at packageResolve (node:internal/modules/esm/resolve:873:14)
    at moduleResolve (node:internal/modules/esm/resolve:929:18)
    at Loader.defaultResolve [as _resolve] (node:internal/modules/esm/resolve:1044:11)
    at Loader.resolve (node:internal/modules/esm/loader:89:40)
    at Loader.getModuleJob (node:internal/modules/esm/loader:242:28)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
    at link (node:internal/modules/esm/module_job:75:36) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Workaround for both Jest and Node.js:

import { parse, stringify } from './node_modules/csv/dist/esm/sync.js';

Yeah, if you expand the details, you can see the problem.

fileName: "/node_modules/csv-parse/dist/cjs/sync.d.cts"
moduleSpecifier: "./index.js"

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 other node-csv packages)

Internal resolution error

Import found in a type declaration file failed to resolve. Either this indicates that runtime resolution errors will occur, or (more likely) the types misrepresent the contents of the JavaScript files.

Screenshot 2023-08-21 at 18 50 53

Had a related problem compiling with pkg. TS using modules would work using require("csv/sync") but pkg cannot find the module. Using require("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 using var { 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.

Version 6.0.5 is also throwing an error with Jest 27 Experimental ESM support + Node v16.11.0 (ESM)

import {generate, parse, transform, stringify} from 'csv/sync';
// ...

This is no longer throwing on Jest 28 🎉 Thanks to support for the exports field in package.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’s no-unresolved rule)

Nice! I guess it should also work with import, using the CommonJS path - in case you prefer that to require.

TS won’t match the declaration file when called with ESM syntax for whatever reason

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?

Me too, with TS and csv-parse. And I don’t want to install whole csv@6.3.2 just 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.

Workaround only gets me this

@bnwa check inside your node_modules folder - I think there is a non-ESM version of the file.

Should be in the node_modules/csv-parse/dist folder, but NOT in the esm folder - look through the other folders for a CommonJS version

Using require and passing the CommonJS path from the dist dir in node_modules worked instead of the ESM dir with import. I thought ts-node was transforming ESM import/export before evaluating deps. Thanks for the tip.

Workaround only gets me this

@bnwa check inside your node_modules folder - I think there is a non-ESM version of the file.

Should be in the node_modules/csv-parse/dist folder, but NOT in the esm folder - look through the other folders for a CommonJS version