node-csv icon indicating copy to clipboard operation
node-csv copied to clipboard

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

Open appfrilans opened this issue 3 years ago • 22 comments

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.

appfrilans avatar Feb 22 '22 17:02 appfrilans

I have never used firebase so I might pass on this one.

wdavidw avatar Feb 22 '22 18:02 wdavidw

Ok. Posted a similar question on SO: https://stackoverflow.com/questions/71232999/error-parsing-triggers-cannot-find-module-csv-parse-sync

appfrilans avatar Feb 23 '22 07:02 appfrilans

I have a similar issue outside of firebase in a plain TypeScript script using version 5.0.4

froesecom avatar Feb 28 '22 21:02 froesecom

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

karlhorky avatar Mar 08 '22 19:03 karlhorky

Am getting the same issue as others have reported attempted to bind dep with import { parse } from 'csv-parse/sync'

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

Workaround only gets me this: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/b/Source/project/node_modules/csv-parse/dist/esm/sync.js require() of ES modules is not supported. require() of /Users/b/Source/project/node_modules/csv-parse/dist/esm/sync.js from /Users/b/Source/project/conversion/src/csv.ts is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules. Instead rename sync.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/b/Source/project/node_modules/csv-parse/package.json.

bnwa avatar Mar 12 '22 01:03 bnwa

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

karlhorky avatar Mar 12 '22 11:03 karlhorky

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.

bnwa avatar Mar 12 '22 16:03 bnwa

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

karlhorky avatar Mar 12 '22 16:03 karlhorky

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

bnwa avatar Mar 12 '22 16:03 bnwa

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)

karlhorky avatar Apr 28 '22 19:04 karlhorky

I have this problem on Expressjs app node version 12+, and also run the project with node 10, 12 , 14 ,16.What I tried:

  1. With versions 5.0.4 and 5.3.0 import parse from 'csv-parse/dist/esm/sync'; import parse from 'csv-parse/lib/sync'; Result: error TS2349: This expression is not callable. const records = parse(fileContent, {

import {parse} from 'csv-parse/dist/esm/sync'; import {parse } from 'csv-parse/lib/sync'; export { CsvError, parse }; ^^^^^^

SyntaxError: Unexpected token export

import {parse } from 'csv-parse/sync'; [INFO] ts-node-dev ver. 1.1.8 (using ts-node ver. 9.1.1, typescript ver. 4.5.4) Error: Cannot find module 'csv-parse/sync'

vladimirpetukhov avatar Aug 08 '22 16:08 vladimirpetukhov

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"
    }
// ...

tfazekas avatar Aug 10 '22 08:08 tfazekas

This is still an issue...

Using latest version 5.3.2 On node 14:

If you do

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

You get

Error: Cannot find module 'csv-parse/sync'

If you do

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

You get

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /<path>/node_modules/csv-parse/lib/sync.js
require() of ES modules is not supported.
require() of /<path>/node_modules/csv-parse/lib/sync.js from <my-script> is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename sync.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /<path>/node_modules/csv-parse/package.json.

To help with debugging, this is my script's package.json

{
  "name": "my-package",
  "version": "0.1.0",
  "dependencies": {
    "csv-parse": "^5.3.2",
  }
}

yellowmamba avatar Nov 15 '22 23:11 yellowmamba

This is a nightmare

wdavidw avatar Nov 16 '22 17:11 wdavidw

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.

shunyun-bp avatar Aug 11 '23 14:08 shunyun-bp

@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

karlhorky avatar Aug 21 '23 16:08 karlhorky

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/[email protected]/dist/cjs/

andrewbranch avatar Aug 21 '23 18:08 andrewbranch

Pretty sure it comes from packages/csv-parse/rollup.config.js but I've never used Rollup so I might be wrong. Any Rollup expert over here?

MaximeBernard avatar Aug 22 '23 14:08 MaximeBernard

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.

wdavidw avatar Aug 24 '23 10:08 wdavidw

Hm, I just tried upgrading to [email protected] (which also includes the changes from https://github.com/adaltas/node-csv/pull/397) and I no longer need an ESLint disable for import/no-unresolved (not sure if this was caused by this change or some other earlier change) - can anyone else confirm that this is fixed now?

karlhorky avatar Aug 25 '23 10:08 karlhorky

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?

MaximeBernard avatar Aug 25 '23 14:08 MaximeBernard

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 [email protected] 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.

nklemiata avatar Jan 11 '24 21:01 nklemiata