Skip to content

Better support cjs #124

Description

@Pcrab

When I tried to use fzf in a commonjs package, it always throws

Error [ERR_REQUIRE_ESM]: require() of ES Module xxx/node_modules/.pnpm/fzf@0.5.2/node_modules/fzf/dist/fzf.umd.js from xxx/index.js not supported.
fzf.umd.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename fzf.umd.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in xxx/node_modules/.pnpm/fzf@0.5.2/node_modules/fzf/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

I've read #85, if I change my package to esm, this can be solved, but the package must finally be compiled to a cjs format.

This can be fixed if the umd format dist file is renamed to fzf.umd.cjs since "type": "module" is added at the end of package.json.

changes to package.json

{
  "main": "./dist/fzf.umd.cjs",
  "exports": {
    ".": {
      "require": "./dist/fzf.umd.cjs"
    }
  }
}

changes to vite-legacy.config.ts

export default defineConfig({
  build: {
    lib: {
      fileName: (format) => `fzf.${format}.cjs`,
    },
  },
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions