Skip to content

Unable to import map-transform in a TypeScript/JS project #131

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

Closed
oderayi opened this issue Sep 25, 2024 · 9 comments
Closed

Unable to import map-transform in a TypeScript/JS project #131

oderayi opened this issue Sep 25, 2024 · 9 comments

Comments

@oderayi
Copy link

oderayi commented Sep 25, 2024

I am unable to use map-transform in a TypeScript project with the following import statement:

import mapTransform from 'map-transform

Error reported is:
Cannot find module 'map-transform' or its corresponding type declarations.ts(2307)

@oderayi oderayi changed the title Unable to import map-transform in a TypeScript Unable to import map-transform in a TypeScript project Sep 25, 2024
@kjellmorten
Copy link
Contributor

Hi, @oderayi!
Thanks for reaching out and for using map-transform.

This works in other repos, so we need a bit more information to pinpoint why it doesn't work in your case. Do you have a public repo where we can see your code?

Also, it sounds like what would happen if you don't do npm install. You have probably done that, but I'm mentioning it just to make sure.

@oderayi
Copy link
Author

oderayi commented Sep 25, 2024

Thanks for the quick response @kjellmorten

I created a JS test-repo here.

Seems map-transform fails in JS projects too.

Simply run the test. The map-transform import in index.test.js should fail.

@oderayi oderayi changed the title Unable to import map-transform in a TypeScript project Unable to import map-transform in a TypeScript/JS project Sep 25, 2024
@kjellmorten
Copy link
Contributor

You're absolutely right and I see the problem now, @oderayi.

map-transform is an ESM package (hence the "type": "module" in package.json), and can therefore only be imported into other ESM packages. This was introduced in v0.4, so it has been with us for a while, but I see that we never mentioned it in the readme. Sorry about that! I'll make sure to add it now.

If you can change your project to be an ESM package too, it should work. If not, I don't think there is a workaround. Sorry again.

@kjellmorten
Copy link
Contributor

@oderayi, I just stumbled upon this, which seems to let you import an ESM package in a non-ESM package. https://nodejs.org/en/blog/announcements/v22-release-announce#support-requireing-synchronous-esm-graphs

@oderayi
Copy link
Author

oderayi commented Sep 26, 2024

Thank you @kjellmorten.
I'm happy to close this as well.
Also, thank you for making this library and keeping it up to date.

@oderayi
Copy link
Author

oderayi commented Sep 26, 2024

Just to add, with a library like tsup, both ESM and CommonJS builds can be produced from a single TS project, with minimal config.

@kjellmorten
Copy link
Contributor

Thanks, @oderayi!

It looks like tsup is not an ESM package itself, they just offer both options for others. So if they need to import ESM packages, they have the same problem that you had here. It's times like this that I consider moving everything to Deno. 😉

@oderayi
Copy link
Author

oderayi commented Sep 30, 2024

@kjellmorten I decided this morning to open a PR to enable CommonJS distribution using tsup as mentioned in my comment above.

Output is the same as with tsc only with .cjs files also exported to allow for compatibility with CommonJS-based projects.

I did this because we ran into a number issues in our project, which includes:

  • having to rely only on dynamic import to import map-transform modules
  • inability to import type declarations from map-transform
  • impracticality of moving our project to ESM

I hope my PR will be considered.

@kjellmorten
Copy link
Contributor

Hi again, @oderayi, and thanks for this!

I've been traveling, so I haven't had the chance to look at this, and to make a change like this I need make sure I understand all the consequences. I'll look at your PR as soon as possible.

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

2 participants