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.

Possible to have `export default` emit as `module.exports`

See original GitHub issue

I know it’s not the preferred way of having a default export, but it would save a lot of time if this would be possible. I’m writing an ESLint plugin and currently cannot import the plugin into a CJS file because the module is exported as module.exports.default rather than simply module.exports.

Have only come across this issue which I wasn’t able to find a way to implement it properly: https://github.com/egoist/tsup/issues/283 and also #255 but I got the warnings below: image

Thanks!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
paambaaticommented, Mar 7, 2022

FWIW, I solved this by adding this to my tsup config –

import { defineConfig } from 'tsup'

export default defineConfig({
  splitting: false,
  sourcemap: false,
  minify: true,
  clean: true,
+  esbuildOptions: (options) => {
+   options.footer = {
+    // This will ensure we can continue writing this plugin
+      // as a modern ECMA module, while still publishing this as a CommonJS
+      // library with a default export, as that's how ESLint expects plugins to look.
+      // @see https://github.com/evanw/esbuild/issues/1182#issuecomment-1011414271
+      js: 'module.exports = module.exports.default;',
+    }
  },
  entry: ['src/index.ts'],
})

See https://github.com/getoslash/eslint-plugin-tap/pull/36 for reference.

0reactions
lvjiaxuancommented, Jul 22, 2022

I know it’s not the preferred way of having a default export, but it would save a lot of time if this would be possible. I’m writing an ESLint plugin and currently cannot import the plugin into a CJS file because the module is exported as module.exports.default rather than simply module.exports.

Have only come across this issue which I wasn’t able to find a way to implement it properly: #283 and also #255 but I got the warnings below: image

Thanks!

I am wondering why it's not the preferred way of having a default export. Could you explain any arguments for me, thanks~~

Read more comments on GitHub >

github_iconTop Results From Across the Web

export - JavaScript - MDN Web Docs
Every module can have two different types of export, named export and default export. You can have multiple named exports per module but ......
Read more >
Difference between destructing export default and module ...
If you export defualt you need to import it like import env from 'location' (Look I emit the {} part) and then access...
Read more >
Understanding Modules and Import and Export Statements in ...
Default Exports​​ Modules can also contain a default export, using the default keyword. A default export will not be imported with curly brackets...
Read more >
Use Named Exports over Default Exports in JavaScript
Default Exports vs Named Exports. The export statement is used when creating JavaScript modules and you want to share objects, functions, ...
Read more >
TSConfig Option: allowSyntheticDefaultImports - TypeScript
This option brings the behavior of TypeScript in-line with Babel, where extra code is emitted to make using a default export of a...
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