Can't resolve 'fs' during require svelte/register

1.5k views Asked by At

Hello I am experimenting with svelte/register: https://svelte.dev/docs#svelte_register

But when I add require('svelte/register'); into my file so I get error:


ERROR in ./node_modules/svelte/register.js
Module not found: Error: Can't resolve 'fs' in '/home/martin/Projects/LearnEnglishWordsV3/node_modules/svelte'
 @ ./node_modules/svelte/register.js 1:9-22
 @ ./src/views/WordListView.svelte
 @ ./src/components/App.svelte
 @ ./src/js/app.js

Build failed with errors.

npm ERR! code ELIFECYCLE
npm ERR! errno 1

Do you anybody know what I have to do for fix it?

1

There are 1 answers

0
Bjorn Lu On BEST ANSWER

require('svelte/register') can only be used in node.js to compile the imported svelte files. It seems that you're calling it in the browser, which it then complains about fs (a node.js module).

If your goal is to compile svelte files in the browser, use svelte.compile, which is also what svelte/register uses under the hood. But do note that it brings a larger bundle size.

Otherwise, you can just safely remove require('svelte/register').