Hi,
I am trying to create a timed redirect from the home page with this js:
import wixLocation from 'wix-location';
$w.onReady(function () {
setTimeout(() => {
wixLocation.to(`/about-us`);
}, 5000);
});
throws: SyntaxError: Unexpected identifier 'wixLocation'. import call expects exactly one argument.
from bootstrap.
I have tried commas, back-tics, and various code. I am settling on this set of code.
Something about importing it as a module?
To solve the error, set the type attribute to module when loading the script in your HTML code. When working with ECMAScript modules and JavaScript module import statements in the browser, you'll need to explicitly tell the browser that a script is module. To do this, you have to add type="module" onto any ‹script› tags that point to a JavaScript module. Once you do this you can import that module without issues.
<script type="module" src="./index.js"></script>
If you are working on Node.js or react applications and using import statements instead of require to load the modules, then ensure your package.json has a property "type": "module" as shown below.
{ // ... "type": "module", // ... }
Moreover, In some cases, you may have to use both import and require statements to load the module properly.
// import { parse } from 'node-html-parser'; parse = require('node-html-parser');
This error "Cannot use import statement outside a module " can happen in different cases depending on whether you're working with JavaScript on the server-side with Node.js , or client-side in the browser. There are several reasons behind this error, and the solution depends on how you call the module or script tag.
Thanks! I'm glad it's not just me :D
Thanks Yisrael :) There is no other custom code. However I noticed that the error is being generated by a bootstrap js file - which must be a template thing? Which additionally says: Uncaught SyntaxError: Cannot use import statement outside a module
I probably shouldn't post a live site to the forums? But to help debug:
eltoromargaretriver dot com dot au
I tried your code and it worked just fine. Where do you have the code? Is there any other code on the page?