0
votes

npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! node-express@1.0.0 start: node server npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the node-express@1.0.0 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in: npm ERR! /Users/teevee/.npm/_logs/2020-05-30T18_42_30_526Z-debug.log

I haven't had any issues until I had to add routes for partners and promotions in my project. I don't know how to debug this type of error in order to make the needed changes. What can possibly be the issue? I'm still learning. Here is a link: https://github.com/TanishaV842/node-express/tree/master/routes

2
Did you try to clear npm cache or deleting, package-lock.json and node_modules?SMAKSS
Not sure, but it looks like in your routes/<type>Router.js, the .route(/<path>) code is also duplicated in your main server.js code. Do you need both route definitions? it seems like one or the other would suffice.mfink
Also, look above your npm ERR! log for the stacktrace, that may lead you to the line in your source code that produced the error, and in many cases, tell you why the error was thrown.mfink

2 Answers

1
votes

One of the issues you have is that it looks like you're including code for your routers in multiple places:

// server.js

app.use("/campsites", campsiteRouter);
app.use("/partners", partnerRouter);
app.use("/promotions", promotionRouter);

campsiteRouter.route("/campsites").all(...);
partnerRouter.route("/partners").all(...);
promotionsRouter.route("/promotions").all(...);

There's no need to do that, just do app.use("/campsites", campsiteRouter); and get rid of the router code inside server.js.


Second, the code inside of your routers is incorrect in two ways:

// these are two different routes, so create a separate
// route for `/campsites/:campsiteId`,
// additionally, you don't want to do `/campsites/:campsiteId` 
// because you specified the `/campsite` route/portion inside of 
// your `server.js` file when you did 
// `app.use("/campsites", campsiteRouter)`, so only use `/:campsiteId`

// WRONG
campsiteRouter
  .route("/campsites")
  .all(...)
  .get("/campsites/:campsiteId", ...)

// CORRECT
campsiteRouter
  .route("/:campsiteId")
  .all(...)
  .get((req, res) => {
    console.log(req.params.campsiteId);
  })
  .post(...);

Here's your fixed code:

Hope this helps.

0
votes

It looks like you're mixing up regular router.get() with router.route().get() method parameters.

Because you're using the router.route() method, your:

.get() .post() .delete()

call can't pass in a string and a callback. They only except a callback function.

To fix,

change all of the router.route(...).get('<path>', (req, res) => {})

to something like:

router.route(...).get((req, res) => {})

No path parameter.