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 [ERR_REQUIRE_ESM]: Must use import to load ES Module: ROOT\node_modules\camelcase-keys\index.js #93

Closed
farhan-shaikh-raw opened this issue Jun 27, 2022 · 7 comments

Comments

@farhan-shaikh-raw
Copy link

Getting error on firebase deploy. Full error below

Error: Failed to load function definition from source: Failed to generate manifest from function source: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\node\codelabs\in_app_purchases\complete\firebase-backend\functions\node_modules\camelcase-keys\index.js require() of ES modules is not supported.

Node Version

14.18.2

firebase Version

11.1.0

tsconfig.json

{ "compilerOptions": { "module": "commonjs", "noImplicitReturns": true, "noUnusedLocals": true, "outDir": "lib", "sourceMap": true, "strict": true, "target": "es2017", "resolveJsonModule": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true }, "compileOnSave": true, "include": [ "src" ] }

Steps followed

https://codelabs.developers.google.com/codelabs/flutter-in-app-purchases#10

Git Repo

https://github.com/flutter/codelabs/tree/master/in_app_purchases/complete/firebase-backend

@sindresorhus
Copy link
Owner

https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

@sindresorhus sindresorhus closed this as not planned Won't fix, can't repro, duplicate, stale Jun 27, 2022
@simplenotezy
Copy link

@sindresorhus How would this work with Nest.js

@simplenotezy
Copy link

I ended up simply using:

_.mapKeys(obj, (v, k) => _.camelCase(k))

Since importing the package was not easily possible with Nest.js unfortunately

@GeylanKalafMohe
Copy link

I ended up simply using:

_.mapKeys(obj, (v, k) => _.camelCase(k))

Since importing the package was not easily possible with Nest.js unfortunately

What is "_" and obj here?

@freddygarcia
Copy link

I ended up simply using:

_.mapKeys(obj, (v, k) => _.camelCase(k))

Since importing the package was not easily possible with Nest.js unfortunately

What is "_" and obj here?

@GeylanKalafMohe Lodash

@isaachinman
Copy link

Hello all. I've created a util to convert ESM-only packages to CJS, for those who need it. This is still very much a work in progress, so please open an issue on the repository with any feedback.

This package (camelcase-keys@8.0.2) can be installed as CJS via @cjs-exporter/camelcase-keys.

@akay64
Copy link

akay64 commented Mar 22, 2024

Because I wanted to use the latest version of this library I used an approach where I used ESBuild to convert this ESM only lib to CJS format. This should work for most of such cases and ideally you wont have to do this often as there aren't many libraries that have removed CJS support. In addition to that doing this for these type small util libraries, you likely don't really need to update ever them unless there is some serious security issue in which case you can use repeat the process again.

So I ran ESBuild, took the built result and the index.d.ts file, put it into a "vendor" directory and import from there. Problem solved if you don't want to migrate off of CJS if your project already works that way. It took 5 to 10 mins max and got back on track.

  1. npm install esbuild
  2. git clone repo of the ESM lib you want to use that does not offer CJS support in this case to ./camelcase-keys
  3. open package.json of that repo
  4. remove the line "type": "module"
  5. Remove "exports": { "types": "./index.d.ts", "default": "./index.js" }
  6. Add "types": "./index.d.ts" and "main": "./index.js"
  7. npx esbuild ./camelcase-keys --bundle --platform="node" --outfile=./static-vendor/camelcase-keys/index.js
  8. import and use in your code like normal import camelcaseKeys from "../static-vendor/camelcase-keys/index.js";
  9. Copy index.d.ts file from cloned repo to ./static-vendor/camelcase-keys/

If you want you can probably alias it or something and make the import cleaner but this is fine for my needs, typing also works perfectly.

Cheers.

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

7 participants