Unable to export a component using es5 require statement
See original GitHub issueThis 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:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
There might be a Rollup plugin or configuration that turns
require()
s intoimport
s, but this would be unrelated to Svelte.As a general rule, I would suggest writing your components with ESM
import
s and have them compile torequire
s where necessary for use on the server.You should do
import gravatar from 'gravatar'