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

Better support cjs #124

Open
Pcrab opened this issue Oct 29, 2023 · 1 comment
Open

Better support cjs #124

Pcrab opened this issue Oct 29, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@Pcrab
Copy link

Pcrab commented Oct 29, 2023

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`,
    },
  },
});
@Pcrab Pcrab added the enhancement New feature or request label Oct 29, 2023
@ajitid
Copy link
Owner

ajitid commented Nov 8, 2023

Heya! I looked at your PR and I can't say for sure if it'll be accepted. I'll have to check in other projects to know if it is an actual convention or format to have .umd.cjs as extension. This will take longer than usual as I'm occupied with other things as well.

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

No branches or pull requests

2 participants