I am working on MERN app and while importing express it is showing error
it shows SyntaxError: Cannot use import statement outside a module
my code is
import express from 'express';
const app = express();
const port = process.env.PORT || 3000;
app.get('/',(req,res)=>{
res.send('this is homepage')
})
app.listen( port ,()=>{
console.log('server is running at port number 3000')
});
import and export are ES6 syntax.
To enable ES6 modules in your NodeJS app add "type": "module" to your package.json file.
Alternatively you can use the .mjs extension for your files.
Otherwise node defaults to the CommonJS module loader which uses require and module.exports syntax.
ES6 module support is stable in Node v12 and above, and in experimental in v9 - v11, requiring passing the --experimental-module flag.
Node.js documentation: Modules: ECMAScript modules
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With