Home > Blockchain >  require is not defined in express js and i tried to solve it with type:module
require is not defined in express js and i tried to solve it with type:module

Time:07-28

the problem is in require is not defined how i solve this problem actually i try to solve it with adding type:module to package.json and convert js to mjs but it still the same problem

app.mjs

const expresss = require("express");
const bodyParser = require("body-parser");
const PORT = 3000


const app = expresss()

app.get("/",(req , res)=>{
    res.send("welcome to our blog")
})

app.listen(PORT,()=>{
    console.log("we are rouning on this server");
})

package.json

{
  "name": "blog",
  "version": "1.0.0",
  "description": "",
  "type" :"module",
  "main": "app.mjs",
  "scripts": {
    "start": "nodemon app.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.20.0",
    "express": "^4.18.1"
  }
}

CodePudding user response:

Maybe try converting it back to a .js file and manualy start it from the command line using the syntax.

node ./app.js

I am not 100% sure if this is the problem, but its worth a shot.

  • Related