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

Cannot be used in a Node.js ESM environment: Named export 'parse' not found #939

Open
cdauth opened this issue Jun 15, 2022 · 2 comments
Open

Comments

@cdauth
Copy link

cdauth commented Jun 15, 2022

When attempting to import PapaParse from a Node.js ES module, the following error is shown:

import { parse } from "../../node_modules/papaparse/papaparse.js";
         ^^^^^
SyntaxError: Named export 'parse' not found. The requested module '../../node_modules/papaparse/papaparse.js' is a CommonJS module, which may not support all module.exports as named exports.

As a workaround, PapaParse can be imported using import papaparse from 'papaparse' and then accessing the function using papaparse.parse. As a workaround in code that is shared by the frontend and backend, PapaParse can be imported in the following way:

import * as papaparse from 'papaparse';
const { parse } = ((papaparse as any).default ?? papaparse) as typeof papaparse;

To fix the issue, PapaParse needs to publish a 3rd bundle in ESM format. The bundle needs to have the .mjs file extension and does not have to be minified or contain any shims or transpilations to support old browsers. Then the following export map needs to be added to package.json:

  "exports": {
    ".": {
      "import": "./papaparse.mjs",
      "require": "./papaparse.js"
    },
    "./*": "./*"
  }

The wildcard export at the end is to provide backwards compatibility with existing projects that are importing PapaParse in unusual ways.

@rauschma
Copy link

Note that name-importing from CommonJS works if the exports look like this (more information):

exports.namedExport = 'yes';

@professorf
Copy link

Try this:

import {default as papa} from "papaparse"

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

3 participants