Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
appfrilans opened this issue Feb 22, 2022 · 22 comments
Open

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

appfrilans opened this issue Feb 22, 2022 · 22 comments

Comments

@appfrilans
Copy link

appfrilans commented Feb 22, 2022

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.

@wdavidw
Copy link
Member

wdavidw commented Feb 22, 2022

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

@appfrilans
Copy link
Author

@froesecom
Copy link

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

@karlhorky
Copy link

karlhorky commented Mar 8, 2022

Edit: Jest 28 no longer has this issue: #323 (comment)


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

@bnwa
Copy link

bnwa commented Mar 12, 2022

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.

@karlhorky
Copy link

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

@bnwa
Copy link

bnwa commented Mar 12, 2022

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.

@karlhorky
Copy link

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

@bnwa
Copy link

bnwa commented Mar 12, 2022

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

@karlhorky
Copy link

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 import-js/eslint-plugin-import#1810 it is still incompatible with eslint-plugin-import's no-unresolved rule)

@vladimirpetukhov
Copy link

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'

@tfazekas
Copy link

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

@yellowmamba
Copy link

yellowmamba commented Nov 15, 2022

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",
  }
}

@wdavidw
Copy link
Member

wdavidw commented Nov 16, 2022

This is a nightmare

@shunyun-bp
Copy link

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.

@karlhorky
Copy link

@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

@andrewbranch
Copy link

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/

@MaximeBernard
Copy link

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?

@wdavidw
Copy link
Member

wdavidw commented Aug 24, 2023

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.

@karlhorky
Copy link

karlhorky commented Aug 25, 2023

Hm, I just tried upgrading to csv@6.3.2 (which also includes the changes from #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?

@MaximeBernard
Copy link

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?

@nklemiata
Copy link

nklemiata commented Jan 11, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests