I have a shell.mjs file with the following function shell():
import { exec } from 'child_process';
export function shell() {
let command_test = 'echo hello';
exec(`${command_test}`, (err, stdout, stderr) => {
if (err) {
//some err occurred
console.error(err)
} else {
// the *entire* stdout and stderr (buffered)
string = `${stdout}`;
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
}
});
}
When run on its own using node shell.mjs, the function works well.
However, when I try to import shell() to a Svelte app (created using this template: https://github.com/sveltejs/template), and run it in the browser using npm run dev (using Rollup), Rollup and the JavaScript console both tell me that they cannot find the 'child_process' module. Here's the error message from Rollup:
(!) Missing global variable name Use output.globals to specify browser global variable names corresponding to external modules child_process (guessing 'child_process')
And from JavaScript:
ReferenceError: Can't find variable: child_process
That's the code that I use in App.svelte to call the shell() function:
<script>
import { shell } from './shell.mjs'
shell();
</script>
App.svelte is then called in main.js:
import App from './App.svelte';
const app = new App({
target: document.body
});
export default app;
How can I fix that?