GithubHelp home page GithubHelp logo

`Error [ERR_REQUIRE_ESM]`: Must use import to load ES Module: `ROOT\node_modules\camelcase-keys\index.js` about camelcase-keys HOT 7 CLOSED

farhan-shaikh-raw avatar farhan-shaikh-raw commented on June 13, 2024
`Error [ERR_REQUIRE_ESM]`: Must use import to load ES Module: `ROOT\node_modules\camelcase-keys\index.js`

from camelcase-keys.

Comments (7)

sindresorhus avatar sindresorhus commented on June 13, 2024 4

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

from camelcase-keys.

simplenotezy avatar simplenotezy commented on June 13, 2024 4

I ended up simply using:

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

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

from camelcase-keys.

isaachinman avatar isaachinman commented on June 13, 2024 3

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 ([email protected]) can be installed as CJS via @cjs-exporter/camelcase-keys.

from camelcase-keys.

simplenotezy avatar simplenotezy commented on June 13, 2024

@sindresorhus How would this work with Nest.js

from camelcase-keys.

GeylanKalafMohe avatar GeylanKalafMohe commented on June 13, 2024

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?

from camelcase-keys.

freddygarcia avatar freddygarcia commented on June 13, 2024

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

from camelcase-keys.

akay64 avatar akay64 commented on June 13, 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.

from camelcase-keys.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.