Hi @BananaLin ,
Cannot use import statement outside a module
You can try the following three methods to solve this problem.
Solution 1 – Add “type”: “module” to package.json.
Adding “type”: “module” to package.json will tell Node you are using ES2015 modules(es modules), which should get solve the error.
{
// ...
"type": "module",
// ...
}
Solution 2 – Add type=”module” attribute to the script tag.
Another reason we get this error is if we are loading the script from the src directory instead of the built file inside the dist directory.
<script type="module" src="some_script.js"></script>
Solution 3 – Use import and require to load the modules
In some cases, we may have to use both import and require statements to load the module properly.
For Example –
import { parse } from 'node-html-parser';
parse = require('node-html-parser');
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.