I am trying to import a file into my another js file (sandbox.js). I am getting above mentioned error in the browser console.
File to be imported: module1.js
const people = ["Chaitanya","Ayush"];
const age = [22,22];
export default {
people,
age
};
Main file: sandbox.js
import fs from 'fs'
fs.readFile('./textfile.txt',(err,data)=>{
console.log(err);
});
HTML file: index.html
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="styles.css">
<title>JavaScript</title>
</head>
<body>
<h1 class="title">The Great List of Mine</h1>
<ul>
<li class="mundane">Read a book</li>
<li class="mundane">Call a friend</li>
<li class="mundane">Study Web designing</li>
<button class="completeAll">Click to complete</button>
<button class="resetAll">Reset</button>
</ul>
<script src="sandbox.js"></script>
</body>
</html>
package.json:
{
"description": "This is a dummy package.json",
"type": "module",
"name": "javascript",
"version": "1.0.0",
"main": "module1.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
When i run the html with a live server and open the console in browser, i get the error:
Uncaught SyntaxError: Cannot use import statement outside a module (at sandbox.js:34:1)
Running sandbox.js in terminal directly by node sandbox
works fine.