-1

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')
});
ChrisG
  • 8,206
  • 4
  • 20
  • 34
  • 1
    is it this https://stackoverflow.com/questions/58211880/uncaught-syntaxerror-cannot-use-import-statement-outside-a-module-when-import – cmgchess May 17 '22 at 12:14
  • Replace `import express from 'express';` with `const express = require('express');` – ChrisG May 17 '22 at 12:15
  • 1
    Node with the extension `.js` will use common.js loader, eg. `require`, if you want to use ES loader, you can rename the file extension to `.mjs`. There are other subtle differences between ES & CommonJS, so you might want check those out.. eg, with ES you won't get the very useful `__dirname`, but there are workarounds for this. – Keith May 17 '22 at 12:22

1 Answers1

0

when you work with nodejs, you should use this syntax instead of import '...' from '....':

const express = require('express')