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

No named or default exports available #36

Closed
cyberwombat opened this issue Nov 20, 2022 · 19 comments
Closed

No named or default exports available #36

cyberwombat opened this issue Nov 20, 2022 · 19 comments

Comments

@cyberwombat
Copy link

cyberwombat commented Nov 20, 2022

Using v ^5.1.3 and:

import { merge } from 'merge-anything'

I get "Module '"merge-anything"' has no exported member 'merge'". I have tried default export as well. I am using type="module" in my package.json as well as this for my TSConfig:

 "target": "es2020",
    "lib": ["es2020"],
    "module": "node16",
    "moduleResolution": "NodeNext",
    "esModuleInterop": true, // Eases ESM support
    "types": ["node"],
    "allowSyntheticDefaultImports": true,

I think there is something missing in package. If I click (using VSCode) on "merge-anything" it takes me to the declaration file:

export * from './merge';
export * from './extensions';

For ESM exports these need extensions. So:

export * from './merge.js'
export * from './extensions.js'

And then it works.

@mesqueeb
Copy link
Owner

That shouldn't be the case. Declaration files are ts, and ts doesn't do extensions.

It's works perfectly fine for me. 🤔
Can you provide a reproduction please ?

@cyberwombat
Copy link
Author

Well esm requires extensions. https://www.typescriptlang.org/docs/handbook/esm-node.html
"relative import paths need full extensions (e.g we have to write import "./foo.js" instead of import "./foo")"

@mesqueeb
Copy link
Owner

the in-point of the package is here: dist/index.es.js

There are no syntax errors in there.

The type declaration file is auto generated and is pure typescript. It has nothing to do with esm. Type declaration files only need to adhere to typescript syntax and are only used by the code editor. Your build pipeline won't even use these.

@cyberwombat
Copy link
Author

ok then.

@mesqueeb
Copy link
Owner

I'm happy to merge PRS or try to work on the issue if you're able to provide me with a reproduction. I'm pretty sure it's something related to the build step of your project. Happy too help out and have a look. Free of charge.

@cyberwombat
Copy link
Author

Well you have tyoe = module. ESM/typescript requires extensions. simple as that. You have the tsconfig I use. Using moduleResolutiion nodeNext I have no idea how that could work for you. Sounds like you are using "node" module resolution and/or older settings. If you have a semi close sandbox I can modify it to show.

@cyberwombat
Copy link
Author

I literally spent hours migrating to esm - so yes extensions are absolutely required (sadly - not a fan of it). The moment I added the extensions to that file above it all worked fine.

@mesqueeb
Copy link
Owner

The issue might be upstream.
the build script I use is rollup with 'rollup-plugin-typescript2':
https://github.com/mesqueeb/merge-anything/blob/production/scripts/build.js

it seems that this plugin does not create the correct .d.ts files.

Can you try contacting there as well?

@mesqueeb mesqueeb reopened this Nov 26, 2022
@cyberwombat
Copy link
Author

Sorry but I moved on to another package - happy to try a new version though

@mesqueeb
Copy link
Owner

Do you use TSC to bundle your package/project? Or do you use something else like esbuild/rollup?

@cyberwombat
Copy link
Author

I use tsx. But should not matter - https://www.typescriptlang.org/docs/handbook/esm-node.html specifically states "relative import paths need full extensions (e.g we have to write import "./foo.js" instead of import "./foo")". There are ways to work around that with various module resolutions but at end of the day that's what needed moving forward with full esm - here's another blog https://2ality.com/2021/06/typescript-esm-nodejs.html covering that.

@mesqueeb
Copy link
Owner

I think we'll have to tweak the usage of the 'rollup-plugin-typescript2' package in the build script. Do you have any hints how outputting .js extensions inside the generated .d.ts files can be achieved ?

@cyberwombat
Copy link
Author

What happens if you add extensions in your code? like src/index.ts:

export * from './merge.js'
export * from './extensions.js'

Does it break things?

@mesqueeb
Copy link
Owner

@cyberwombat i am at a Sinterklaas event today but will try when I get home tonight !!

@mesqueeb
Copy link
Owner

mesqueeb commented Dec 1, 2022

@cyberwombat it worked by manually adding .js.
fixed in v5.1.4

I wonder if VSCode has some auto complete for this, because I'm afraid I might forget in the future 🤔
Or perhaps some ESLint that can throw an error when .js was forgotten.

@cyberwombat
Copy link
Author

VSCode should add extensions manually if package is type module which you have and tsconfig has certain settings (which I posted above). My VSCode certainly does it automatically and also shows errors if not (that might be eslint doing that). Thank you for fixing that!

@cyberwombat
Copy link
Author

Tried it and it works. One thing I did notice - it seems I have to cast the result as unknown then as my return type which seems cumbersome - not sure if it can be made easier.

@mesqueeb
Copy link
Owner

mesqueeb commented Dec 3, 2022

TS should merge the types correctly. Can you open a new issue with the objects you put into merge that give you the faulty type ? I will take a look and see where it's going wrong.

@cyberwombat
Copy link
Author

I'll see if I can - it happens when I am using complex imported types from some aws lib so not super easy to show.

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