DEV Community

ChuangWANG
ChuangWANG

Posted on

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

Top comments (0)