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

deepMerge is not a function #22

Closed
SettingDust opened this issue Nov 7, 2022 · 19 comments
Closed

deepMerge is not a function #22

SettingDust opened this issue Nov 7, 2022 · 19 comments

Comments

@SettingDust
Copy link

The compiled js can't use

{
  default: [Function: merge] {
    options: { mergeArrays: true },
    withOptions: [Function (anonymous)]
  }
}

TypeError: deepMerge is not a function
@voodoocreation
Copy link
Owner

Hey - how are you using it?

If you're using it within some CommonJS code with require, you may need to do this instead:

const merge = require("ts-deepmerge").default;

Let me know if that helps. This package was designed for use within TS codebases with import/export syntax so it hasn't been thoroughly tested outside of that.

@SettingDust
Copy link
Author

SettingDust commented Nov 8, 2022

I'm using it in typescript. And I think I haven't compiled it to cjs. I'm looking into that
.swcrc

{
  "module": {
    "type": "es6"
  },
  "jsc": {
    "parser": {
      "syntax": "typescript"
    }
  },
  "sourceMaps": true
}

tsconfig

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ES2022",
    "esModuleInterop": true,
    "moduleResolution": "Node",
    "noImplicitOverride": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "types": [
      "mocha"
    ]
  },
  "ts-node": {
    "swc": true
  }
}

@voodoocreation
Copy link
Owner

voodoocreation commented Nov 8, 2022

Can you show me how you're importing it and attempting to use it? I've never encountered this sort of issue within a TS codebase before - it generally just works like this without any issues, so it may be something to do with the way your project is being compiled?

import merge from "ts-deepmerge";

const obj = merge({ a: true }, { b: false });

I don't suppose your issue is being caused by allowSyntheticDefaultImports being set to true?

@SettingDust
Copy link
Author

SettingDust commented Nov 8, 2022

I guess the ts-swc-loader Im using for test with mocha caused that. I'm trying to dig it. I don't know if it is respect my swc config.

I think it isn't a problem of code

import deepMerge from 'ts-deepmerge'
deepMerge({ title }, ...it)

@voodoocreation
Copy link
Owner

voodoocreation commented Nov 8, 2022

Hmm it seems like something specific to your config/setup is impacting it somehow and causing the module to return the CommonJS module object rather than the default export from the module which would require you to import it like this instead:

import { default as merge } from "ts-deepmerge";

Would be good to try and track down what's causing it to behave differently though 🤔

Does removing the allowSyntheticDefaultImports property from your tsconfig.json help at all?

@SettingDust
Copy link
Author

SettingDust commented Nov 8, 2022

What's compiled manually(IDK if it as same as the loader's). Will it caused it?

import deepMerge from "ts-deepmerge";
deepMerge.apply(void 0, [
{
  title: title
}
].concat(_toConsumableArray(it)));

@SettingDust
Copy link
Author

Does removing the allowSyntheticDefaultImports property from your tsconfig.json help at all?

Noop

Hmm it seems like something specific to your config/setup is impacting it somehow and causing the module to return the CommonJS module object rather than the default export from the module which would require you to import it like this instead:

import { default as deepMerge } from "ts-deepmerge"; not working

@voodoocreation
Copy link
Owner

I'm not too sure what you mean there. I just know that when consumed normally this issue doesn't occur. Has been tested in a number of different contexts (eg: using the TS compiler on its own, using Webpack etc) and they all resolve as expected.

You can test it out in this bare-bones TS template repo of mine if you like and perhaps compare your config to that, because that one works. It's quite hard to debug your project without having access to it I'm afraid... It just seems like something within your stack is causing the module to return an object instead of the actual module.

@voodoocreation
Copy link
Owner

@SettingDust did you get this working in the end? Just wondering if I can close the issue 🙏

@SettingDust
Copy link
Author

@SettingDust did you get this working in the end? Just wondering if I can close the issue 🙏

No. I've switched from swc to esbuild and still error. Still testing

@SettingDust
Copy link
Author

SettingDust commented Nov 8, 2022

Can't explain the reason.
I guess it caused by exports.default which is a babel proposal(of course typescript use it too).
Maybe the project should be compiled to cjs(module.exports) and esm(export default)

Edit: seems there is a plugin to solve this https://www.npmjs.com/package/swc-plugin-node-cjs-interop

@voodoocreation
Copy link
Owner

Does that plugin enable it to work in your project? We have roughly 800 public GitHub repos depending on this package and none of them have had this issue, so it seems like it's a project configuration issue rather than an issue with the package itself.

If that plugin has allowed it to work, please let me know and I'll close this issue.

@SettingDust
Copy link
Author

SettingDust commented Nov 8, 2022

It's fine with https://github.com/qnighy/node-cjs-interop. The swc plugin not working qnighy/node-cjs-interop#43

@krailler
Copy link

krailler commented Dec 7, 2022

I have same issue :(

@thyming
Copy link

thyming commented Jan 25, 2023

I have the same issue too. I made a reproduction that I think boils down to having "type": "module" in the consuming project's package.json. With it, typescript is happy to compile the code using this library, but the compiled code fails at runtime. Without it, everything works correctly.

@thyming
Copy link

thyming commented Jan 25, 2023

This seems to be a very similar problem and they have a workaround that may work here too without complicating the build hugely.

@voodoocreation
Copy link
Owner

Hmmm interesting... I'll have a look and see what I can do! 👀

@voodoocreation
Copy link
Owner

voodoocreation commented Jan 27, 2023

I've just published a new version which now properly supports consumers that use either ESM or CommonJS syntax.

Once you update to version 6.0.2 (fixed a couple of CJS bugs from the initial v6 release), it should now be able to automatically use our packaged ESM code in your native ESM projects without encountering this issue 🙏

@thyming
Copy link

thyming commented Jan 27, 2023 via email

flostadler added a commit to pulumi/pulumi-eks that referenced this issue Sep 20, 2024
Now that the EKS addons are added we need to align them and do some
cleanup. This involves:
- adding the enums introduced in
#1357 to the VPC CNI
- exposing configurationValues for coredns and kube-proxy
- removing kubectl from the provider
- deeply sort addon configuration keys to guarantee stable json
serialization
- remove deepmerge again. It caused issues during unit tests
(voodoocreation/ts-deepmerge#22) and when used
on outputs.

Additionally I discovered and fixed an old bug that luckily never
surfaced. The VPC CNI configuration did incorrectly handle outputs and
called `toString` on them in a couple of places. The increased type
safety and tests around addon configuration uncovered this.

Closes #1369
flostadler added a commit to pulumi/pulumi-eks that referenced this issue Oct 17, 2024
Now that the EKS addons are added we need to align them and do some
cleanup. This involves:
- adding the enums introduced in
#1357 to the VPC CNI
- exposing configurationValues for coredns and kube-proxy
- removing kubectl from the provider
- deeply sort addon configuration keys to guarantee stable json
serialization
- remove deepmerge again. It caused issues during unit tests
(voodoocreation/ts-deepmerge#22) and when used
on outputs.

Additionally I discovered and fixed an old bug that luckily never
surfaced. The VPC CNI configuration did incorrectly handle outputs and
called `toString` on them in a couple of places. The increased type
safety and tests around addon configuration uncovered this.

Closes #1369
flostadler added a commit to pulumi/pulumi-eks that referenced this issue Oct 17, 2024
Now that the EKS addons are added we need to align them and do some
cleanup. This involves:
- adding the enums introduced in
#1357 to the VPC CNI
- exposing configurationValues for coredns and kube-proxy
- removing kubectl from the provider
- deeply sort addon configuration keys to guarantee stable json
serialization
- remove deepmerge again. It caused issues during unit tests
(voodoocreation/ts-deepmerge#22) and when used
on outputs.

Additionally I discovered and fixed an old bug that luckily never
surfaced. The VPC CNI configuration did incorrectly handle outputs and
called `toString` on them in a couple of places. The increased type
safety and tests around addon configuration uncovered this.

Closes #1369
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

4 participants