0
votes

I have a server, the code of the file index.js is shown below. I want to post it on https://dashboard.heroku.com/. But it does not start the command node inde.js.

import http from 'http'
import express from 'express'
import bodyParser from 'body-parser';


const port = process.env.PORT || 8081;
const app = express();
const server = http.createServer(app);

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));

app.get('/', (req, res) => res.status(200).send({
    message: 'Welcome',
}));

server.listen(port, () => {
    console.log(`Server running at ${port}`);
});

How to run it using the ES6 standard, command node inde.js? I have installed babel7

1
The question isn't specific enough. What does it does not start the command mean? Node already supports ES6 for a long time. The thing you likely refer to are ES Modules.Estus Flask
My code does not start with the command node inde.js. Gives an error message import http from 'http' ^^^^ SyntaxError: Unexpected identifier. How to run my code with the command node inde.js?MegaRoks

1 Answers

1
votes

The ES6 import is not yet fully supported by Node. It is still experimental. To use them, you need to change your files extension from .js to .mjs and start them with this flag :

node --experimental-modules my-app.mjs

More details there : https://nodejs.org/api/esm.html