question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No default export provided

See original GitHub issue

Describe the bug

Many packages import uuid as:

const uuid = require('uuid');

When converted into ES modules, this would correspond to:

import uuid from 'uuid';

When running the browser though, the browser ES module build does not support a “default” export and as a result packages on jspm using UUID are currently breaking.

How to reproduce

Try, for example, import('https://jspm.dev/@google-cloud/bigquery')

Expected behavior

UUID should be configured such that the shape of the module between browsers and Node.js remains the same - the default export should exist for both. Node.js CommonJS modules are always represented by a default export, see https://nodejs.org/dist/latest-v15.x/docs/api/esm.html#esm_commonjs_namespaces for more info.

Additional information

The suggested fix would be to add an export default uuid to the ES module namespace.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
guybedfordcommented, Apr 20, 2021

I wanted to follow up on this issue - I’ve come around to the view of @sokra and @lukastaegert that allowing ES module imports into CommonJS should support different shapes than just { default }.

I’ve since implemented something similar to the requireReturnsDefault: 'preferred' option in RollupJS for JSPM (https://github.com/rollup/plugins/tree/master/packages/commonjs#requirereturnsdefault). And cases like import('https://jspm.dev/@google-cloud/bigquery') are working now with uuid being required as an ES module as require('uuid') being rewritten into:

import * as _uuid from 'uuid';
var uuid = 'default' in _uuid ? _uuid.default : _uuid.

I think there is still some clear incompatibility on these paths between tools as Webpack has one way, RollupJS has a default and lots of options, and I’m not sure what esbuild does. A clear investigation into these cases may be found in Tobias’s compat table where the risks can be assessed but I haven’t been able to fully check that.

Hopefully we can work towards this case being very clearly supported between tools, or at least have a very clear subset for it.

As to whether the PR should be merged, it would likely be a safer compatibility to still merge it, but could be fine to leave as well now.

I’m closing this issue as resolved since I believe it seems tools will ensure library authors shouldn’t have to be too concerned about this, but let’s continue discussion if anyone is interested too.

0reactions
guybedfordcommented, Feb 28, 2021

Seriously though, the fix looks good, and just requires the single require wrapper doing:

export * from './index-browser.mjs';
import * as m from './index-browser.mjs';
export default m;

then the exports in order as you have on the default with the others pointing to the current browser index.

Read more comments on GitHub >

github_iconTop Results From Across the Web

eslint-plugin-import/no-default-export.md at main - GitHub
ESLint plugin with rules that help validate proper imports. - eslint-plugin-import/no-default-export.md at main · import-js/eslint-plugin-import.
Read more >
error TS1192: Module '" A.module"' has no default export
This was a simple error. It occurred due to the missing {} around the module name in the import statement itself.
Read more >
no-default-export - Rule
Rule: no-default-export. Disallows default exports in ES6-style modules. Use named exports instead. Rationale. Named imports/exports promote clarity.
Read more >
Change: Prefer default export to no default export (#20) · Issues
Adding an export to a module will no longer lead to awkward mixes of named and default exports. Or - worse - having...
Read more >
Avoid Export Default - TypeScript Deep Dive - Gitbook
Avoid Export Default · If you refactor Foo in foo.ts it will not rename it in bar.ts . · If you end up...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found