Search code examples
javascriptnode.jsnpmnodemon

How do I fix this error - [nodemon] app crashed - waiting for file changes before starting...?


I'm working on a project with some friends and wanting to start a project with npm run development I get this error:

    const requireSignin = Object(express_jwt__WEBPACK_IMPORTED_MODULE_2__["expressJwt"])({
                                                                                    ^

TypeError: Object(...) is not a function
    at Module.eval (webpack:///./server/controllers/auth.controller.js?:58:85)
    at eval (webpack:///./server/controllers/auth.controller.js?:95:30)
    at Module../server/controllers/auth.controller.js (/Users/mihailomitrovic/Downloads/iteh_projekat/dist/server.generated.js:420:1)
    at __webpack_require__ (/Users/mihailomitrovic/Downloads/iteh_projekat/dist/server.generated.js:21:30)
    at Module.eval (webpack:///./server/routes/user.routes.js?:5:86)
    at eval (webpack:///./server/routes/user.routes.js?:36:30)
    at Module../server/routes/user.routes.js (/Users/mihailomitrovic/Downloads/iteh_projekat/dist/server.generated.js:540:1)
    at __webpack_require__ (/Users/mihailomitrovic/Downloads/iteh_projekat/dist/server.generated.js:21:30)
    at Module.eval (webpack:///./server/express.js?:17:77)
    at eval (webpack:///./server/express.js?:165:30)
    at Module../server/express.js (/Users/mihailomitrovic/Downloads/iteh_projekat/dist/server.generated.js:468:1)
    at __webpack_require__ (/Users/mihailomitrovic/Downloads/iteh_projekat/dist/server.generated.js:21:30)
    at eval (webpack:///./server/server.js?:3:66)
    at Module../server/server.js (/Users/mihailomitrovic/Downloads/iteh_projekat/dist/server.generated.js:552:1)
    at __webpack_require__ (/Users/mihailomitrovic/Downloads/iteh_projekat/dist/server.generated.js:21:30)
    at eval (webpack:///multi_./server/server.js?:1:18)
[nodemon] app crashed - waiting for file changes before starting...

I've tried looking for solutions and looking at parts of the code but nothing has helped so far. Does it have something to do with importing a module? Can someone please help or at least point me in the right direction?

Since it's referring me to this file, this is the code from auth.controller.js:

import User from '../models/user.model'
import jwt from 'jsonwebtoken'
import { expressJwt } from 'express-jwt'
import config from './../../config/config'

const signin = async (req, res) => {
  try {
    let user = await User.findOne({
      "email": req.body.email
    })
    if (!user)
      return res.status('401').json({
        error: "User not found"
      })

    if (!user.authenticate(req.body.password)) {
      return res.status('401').send({
        error: "Email and password don't match."
      })
    }

    const token = jwt.sign({
      _id: user._id
    }, config.jwtSecret)

    res.cookie("t", token, {
      expire: new Date() + 9999
    })

    return res.json({
      token,
      user: {
        _id: user._id,
        name: user.name,
        email: user.email
      }
    })

  } catch (err) {

    return res.status('401').json({
      error: "Could not sign in"
    })

  }
}

const signout = (req, res) => {
  res.clearCookie("t")
  return res.status('200').json({
    message: "signed out"
  })
}

const requireSignin = expressJwt({
  secret: config.jwtSecret,
  userProperty: 'auth'
})

const hasAuthorization = (req, res, next) => {
  const authorized = req.profile && req.auth && req.profile._id == req.auth._id
  if (!(authorized)) {
    return res.status('403').json({
      error: "User is not authorized"
    })
  }
  next()
}

export default {
  signin,
  signout,
  requireSignin,
  hasAuthorization
}

Solution

  • The problem is caused by importing express-jwt.

    You need to replace it with this:

    import { expressjwt } from 'express-jwt'
    

    and you need to pass algorithms as an option when invoking expressjwt:

    expressjwt({
      secret: config.jwtSecret,
      userProperty: 'auth', // and what is this? I'm not sure 'express-jwt' accepts this option
      algorithms: ["HS256"]
    })
    

    You may need to read the doc on how to use express-jwt.