I am Facing the problem of how to use import/export syntax in my scripts, but only on the client-side(Frontend). I want to add scripts to my HTML that will be rendered from Node.js.For example, I want to have multiple script.js that represent modules, and to be able to import/export and render properly. .I do understand that code needs to be compiled to ES6 but having a problem finding how to do it. Thanks in advance.
example from this link: Loading javascript file in node?
var http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');
http.createServer(function(req, res) {
if (req.url === '/') {
fs.readFile('./index.html', function(err, data) {
if (err){
throw err;
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(data);
res.end();
return;
});
} else if (req.url === '/script.js') {
fs.readFile('./script.js', function (err, data) {
if (err) { throw err; }
res.writeHead(200, { 'Content-Type': 'text/javascript' });
res.write(data);
res.end();
return;
});
}
}).listen(3000);
Edit: Already tried to install Babel in the same project, but facing the error : Uncaught SyntaxError: Cannot use import statement outside a module