-
Notifications
You must be signed in to change notification settings - Fork 203
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 default export in Node (immediately overwritten) #36
Comments
I'm using it in Node v8.1.0 and this works: const Hashids = require('hashids'); How are you using it? |
Yes, that works, because CommonJS' However, when used from within transpiled code that expects ES6 module APIs, it breaks: import Hashids from 'hashids' The above will only work if you use the babel interop that uses either the However, the original code for hashids suggests a default export. According to the spec, this should transpile to: module.exports.default = Hashids; The problem with the above however would be that it would break your current syntax: const Hashids = require('hashids'); i.e. a client would have to explicitly use: const Hashids = require('hashids').default; There's a way to support both, though. module.exports = Object.assign(Hashids, {
__esModule: true,
default: HashIds,
}) The culprit seems to be with the choice of the babel plugin, the outdated Replacing with |
I had no success with typescript because of this issue. Had to comment out the last line of the library to make it work without errors:
Without the modification I get an error: |
In Typescript you can still use the |
@cormacrelf only if you target Node. All other forms are broken: |
@niieani I don’t target node. I use Webpack via |
@cormacrelf yeah, either Node or if you use Webpack. CodeSandbox doesn't support requires in TypeScript though. |
I encountered the same issue with Typescript / Angular. Installed the library and typings via npm:
Importing it as follows results in
@cormacrelf's workaround worked for me:
DefinitelyTyped/DefinitelyTyped#15920 might be related. |
My workaround in typescript: import Hashids, * as HashidsConstructor from 'hashids';
//...
const hashids: Hashids = new (HashidsConstructor as any)(); |
Doesn't import * as Hashids from 'hashids' Solve the issue? |
@gDelgado14 no, because typings (and the code) defined a "default" export. Therefore the typings, which are 1:1 with the source code (but not 1:1 with the wrongly transpiled code) expose such a default export. With your code, constructor is available at |
I think the proper declaration file might look something like that of redux-logger. |
@gDelgado14 yes, changing the TypeScript declaration is one way of fixing the TypeScript issue. But it doesn't change the fact that the transpiled code is nonsensical, caused by the deprecated See my previous comment for a solution #36 (comment). |
I could use some help with this. What's the best way to set this up without introducing breaking changes? |
@ivanakimov I've fixed the issue in a backwards compatible way in #53. |
Fix overwritten export (#36) by adding a backwards compatible dist/hashids.js fallback file
@anisabboud I didn't do anything, it was all @niieani. New version will go out soon, I just want to give it a few more days in case anyone else spots any issues. |
Fix overwritten export (#36) by adding a backwards compatible dist/hashids.js fallback file
Hi @ivanakimov.
The code generated here is quite odd. It proceeds to set the default export as Hashids class, and immediately after overwrites the exports with the same, previously defined default export. The result is that there is no default export for ES6, but only a root CommonJS export.
This will of course work if you use something like babel, which emits an interop between default (ES6) and root exports, but will not work if you consume the code directly, e.g. from Node or TypeScript.
A backwards compatible fix would be to add a 'default' property to the class, which points to itself.
The text was updated successfully, but these errors were encountered: