import & require

chuangwang

ChuangWANG

Posted on April 30, 2021

import & require

fix: SyntaxError: Cannot use import statement outside a module

Follow is ok.

let test = require('mathjs')

let result = test.round(3.12484, 3)               

console.log(result)
Enter fullscreen mode Exit fullscreen mode

But import syntax is error:

import { round } from 'mathjs'
let test = round(3.1515,2)
console.log(test)
Enter fullscreen mode Exit fullscreen mode

Solution :

In your package.json file, simply add “type”:“module” to fix this issue :

image

Or you can add the below line in your main entry JS file:

<script type="module" src="main.js"></script>
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
chuangwang
ChuangWANG

Posted on April 30, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related