I am trying to create a simple request However I am getting the following error
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module 'C:\Users\wgupta\Backend\temp\index.js'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
This is the code I have written
const app = require('express')()
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.post('/', (req, res, next) => {
console.log(req.body)
res.json(req.body.adMeta);
})
const port = 3000;
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
CodePudding user response:
try following command
remove node_modules and package-lock.json
rm -rf node_modules package-lock.json
then run following command to install dependencies
npm install
finally, run your package by following command.
npm start
CodePudding user response:
i would look better at package.json file, you are using require -> so have proper type setted in package.json. Because nothing really look wrong w ur code. Are you having the packages in node_modules or any diffrent way?
CodePudding user response:
There might be some mistake in the package.json. Also, I see something off in the code with express.
const express = require('express');
const app = express.app();`
const bodyParser = require('body-parser')
app.use(bodyParser.json()) // no need to use body parser in express 4
app.use(bodyParser.urlencoded({ extended: true }))
app.post('/', (req, res, next) => {
console.log(req.body)
res.json(req.body.adMeta);
})
const port = 3000;
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
Note: try to install all dependencies correctly. If you are using express-generator for the project, you need to run different command to run the project else use node index.js
.