1

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}`)
})
1
  • 1
    Sounds like you are just running node index.js in a directory which doesn't have an index.js in it. Commented Jun 27, 2022 at 13:19

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.