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.

Unable to export a component using es5 require statement

See original GitHub issue

This is maybe a bad configuration issue, but I didn’t find any useful help on my search.

I have a Nav.svelte component using gravatar:

<script>
  import Tailwindcss from '../Tailwindcss.svelte';
  const gravatar = require('gravatar')

  export let homeUrl;
  export let gravatarEmail;

  $: gravatarUrl = gravatar.url(gravatarEmail, { d: 'retro' });
</script>

This component is exported onto a npm package created from this template months ago. Here is the current Rollup configuration:

import svelte from 'rollup-plugin-svelte';
import resolve from '@rollup/plugin-node-resolve';
import pkg from './package.json';

const preprocess = require('./preprocess');

const name = pkg.name
  .replace(/^(@\S+\/)?(svelte-)?(\S+)/, '$3')
  .replace(/^\w/, (m) => m.toUpperCase())
  .replace(/-\w/g, (m) => m[1].toUpperCase());

export default {
  input: 'src/index.js',
  output: [
    {
      file: pkg.module,
      format: 'es',
    },
    {
      file: pkg.main,
      format: 'umd',
      name,
    },
  ],
  plugins: [
    svelte({ preprocess }),
    resolve(),
  ],
};

The compilation “works” but just put a require on the index.mjs file:

function instance($$self, $$props, $$invalidate) {
	const gravatar = require("gravatar");
	let { homeUrl } = $$props;
	let { gravatarEmail } = $$props;

	$$self.$$set = $$props => {
		if ("homeUrl" in $$props) $$invalidate(0, homeUrl = $$props.homeUrl);
		if ("gravatarEmail" in $$props) $$invalidate(2, gravatarEmail = $$props.gravatarEmail);
	};

	let gravatarUrl;

	$$self.$$.update = () => {
		if ($$self.$$.dirty & /*gravatarEmail*/ 4) {
			 $$invalidate(1, gravatarUrl = gravatar.url(gravatarEmail, { d: "retro" }));
		}
	};

	return [homeUrl, gravatarUrl, gravatarEmail];
}

class Nav extends SvelteComponent {
	constructor(options) {
		super();
		if (!document.getElementById("svelte-1pv65z1-style")) add_css$1();
		init(this, options, instance, create_fragment, safe_not_equal, { homeUrl: 0, gravatarEmail: 2 });
	}
}

Resulting on a browser error:

Uncaught ReferenceError: require is not defined

I don’t know how to resolve this issue. Is this referenced somewhere on the official documentation? 🤔

Thanks

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Conduitrycommented, Oct 27, 2020

There might be a Rollup plugin or configuration that turns require()s into imports, but this would be unrelated to Svelte.

As a general rule, I would suggest writing your components with ESM imports and have them compile to requires where necessary for use on the server.

0reactions
benmccanncommented, Oct 28, 2020

You should do import gravatar from 'gravatar'

Read more comments on GitHub >

github_iconTop Results From Across the Web

React js ES5 module.exports issue - Stack Overflow
I used var React = module.exports = require("react"); in main.jsx file. ... But I can't React module use in another class.
Read more >
Module Exports and Loading: ES5 to ES6 | by Zach Gavin
So how were module.exports and require working to share code ... off the bat: require is OUT as with module, import is IN,...
Read more >
Get syntax error when importing into a React app #690 - GitHub
React definitely doesn't require ES5. I think the export just needs some work. Exports can be tricky.
Read more >
JavaScript Require vs. Import - Bits and Pieces
ES Modules use import and export statements to deal with modules. It resolves one of the biggest limitations of CommonJS, which is synchronous ......
Read more >
JavaScript modules - MDN Web Docs
javascript.statements.export. Report problems with ... They need to be top-level items; you can't use export inside a function, for example.
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