Skip to content

deepmerge_1.default) is not a function (No Webpack) #255

Open

Description

@douglasg14b
import deepmerge from 'deepmerge';

deepmerge({}, {});

And ran inside a Jest test, results in:

TypeError: (0 , deepmerge_1.default) is not a function

No webpack, vite, or anything else involved.

Activity

  1. douglasg14b commented on Jan 10, 2023

    @douglasg14b
    Author

    Using the require syntax seems to cause some other issues, with there not being typescript types for it?
    image

  2. ob-aion commented on Aug 29, 2023

    @ob-aion

    I have the same issue when using deepmerge in a dockerized REST API using NestJS.

    Using

    import deepmerge from 'deepmerge';
    
    // ...
    
    deepmerge(car, body, mergeOptions);

    Is throwing the following error
    (0 , deepmerge_1.default) is not a function

    Any update on this?

  3. ob-aion commented on Aug 29, 2023

    @ob-aion

    @douglasg14b one common way to fix this if not already done is to import the library like this:

    import * as deepmerge from 'deepmerge';

    If your code is compiled somehow somewhere, it will basically tell your compiler to expect a CommonJS require and not an ESModule import which would look for a default property added to the deepmerge reference in your code once compiled.

    Hope it helped.

  4. jorenbroekema commented on Dec 19, 2023

    @jorenbroekema

    This module is CJS-only, see #250 .

    As far as I know you can do this in ESM files to import CJS modules, but this only works in NodeJS, not in the browser for example:

    import { createRequire } from 'node:module';
    
    const require = createRequire(import.meta.url);
    const deepmerge = require('deepmerge'); // Function

    Vice versa, in CJS files you can use dynamic imports to asynchronously import ESM, just FYI.

    If you have to use this library in the browser and don't want to use bundlers to transform it and complicate your local dev setup that way, you can use my ESM fork of this lib: https://www.npmjs.com/package/@bundled-es-modules/deepmerge (Github repo)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions