I have a js file looks like this. I want this file works for both
<script src="./foo.js"><script>
and
<script type="module" src="index.js"><script>
foo.js
const foo = () => console.log('Hello World!');
if (typeof module !== 'undefined') {
module.exports = foo;
}
export default foo;
index.js
import foo from './foo.js';
foo();
But this doesn't work, it gives me an error while I'm using <script src="./foo.js"><script> syntax to import my foo.js.
Uncaught SyntaxError: Unexpected token 'export'
Is there a way to make my foo.js works for both of the situations?