Home > OS >  I get a TypeError: express.json() is not a function
I get a TypeError: express.json() is not a function

Time:10-08

I'm a beginner and I've been using github-learning-lab. It states to use app.use(bodyParse.json()) to perform a POST request, but VSCode states that 'body-parse' is deprecated.

I searched online and most people say to use app.use(express.json()) for the current version of Express

My express version: 4.17.1

My node version: 14.17.0

const express = require('express');
const app = express();

app.use(express.json());
app.use(express.urlencoded({ extended: false }))

const mockUserData = [
    {name: 'Mark'},
    {name: 'Jill'}
]
app.get('/users', function(req, res){
    res.json({
        success: true,
        message: 'successfully got users. Nice!',
        users: mockUserData
    })
})
// colons are used as variables that can be viewed in the params
app.get('/users/:id', function(req,res){
    console.log(req.params.id);
    res.json({
        success: true,
        message: 'got one user',
        user: req.params.id
    })
})

app.post('/login', function(req,res){
    // Typically passwords are encrypted using something like bcrypt before sending to database
    const username = req.body.username;
    const password = req.body.password;

    // This should come from the database
    const mockUsername = 'billyTheKid';
    const mockPassword = 'superSecret';

    if (username === mockUsername && password === mockPassword) {
        // In practice, use JSON web token sign method here to make an encrypted token
        res.json({
            success: true,
            message: 'password and username match!',
            token: 'encrypted token goes here'
        })
    } else {
        res.json({
            success: false,
            message: 'password and username do not match'
        })
    }
})

app.listen(8000, function(){
    console.log('server is running')
})
Macs-MBP:node-express-course mac$ node server.js
/Users/mac/Documents/node-express-course/server.js:4
app.use(express.json());
                ^

TypeError: express.json is not a function
    at Object.<anonymous> (/Users/mac/Documents/node-express-course/server.js:4:17)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Macs-MBP:node-express-course mac$ head node_modules/express/package.json
{
  "_from": "express",
  "_id": "[email protected]",
  "_inBundle": false,
  "_integrity": "sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==",
  "_location": "/express",
  "_phantomChildren": {},
  "_requested": {
    "type": "tag",
    "registry": true,

CodePudding user response:

I reinstalled express. I may have made a mistake when installing it initially. I should've tried this from the start. Thanks for the help.

CodePudding user response:

First you could use Bodyparser or you should reinstall it, but I recommend using Bodyparser

  • Related