Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Asynchronous Code in Express Asynchronous Code in Express Using Callbacks

I can't start the server

When I run npm start, it returns a bunch of errors, even though I've tried all the suggestions in the other question about this. How can I fix this? app.js

const express = require('express');
const fs = require('fs');
const path = require('path');

const app = express();

app.set('view engine', 'pug');
app.set("views", path.join(__dirname, "views"));
app.use(express.static('public'));

//CALL BACKS
function getUsers(cb){
  fs.readFile('data.json', 'utf8', (err, data) => {
    if (err) return cb(err);
    const users = JSON.parse(data);
    return cb(null, users);
  });
}

app.get('/', (req,res) => {
  getUsers((err, users) => {
    if (err) {
      res.render('error', {error: err});
    } else {
      res.render('index', {title: "Users", users: users.users})
    }
  });
}); 


app.listen(3000, () => console.log('App listening on port 3000!'));

package.json:

{
  "name": "async-express",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "nodemon"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.4",
    "pug": "^2.0.3"
  },
  "devDependencies": {
    "nodemon": "^1.18.9"
  }
}

Can someone help? Thanks.

Hi babyoscar,

Will you please share the error logs.

Thanks,

Sure, it says

npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path C:\Users\babyoscar\package.json
npm ERR! errno -4058
npm ERR! enoent ENOENT: no such file or directory, open 'C:\Users\babyoscar\package.json'
npm ERR! enoent This is related to npm not being able to find a file.   
npm ERR! enoent

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\babyoscar\AppData\Roaming\npm-cache\_logs\2020-12-19T19_00_49_433Z-debug.log

From above log what I'm able to figure out is you're running command "npm start" in wrong path.

As log says

 no such file or directory, open 'C:\Users\babyoscar\package.json'

Make sure you're running the command in right place and have all the dependencies install before doing npm start.

Hmm, I ran npm install last time, and I'm doing it in the app.js file. Am I supposed to do something else?

3 Answers

in your start script can you try "npx nodemon" instead?

Thanks! It worked.

Let say project structure is something like this

 project
    -app.js
    -package.json

For something like this

 project
   src
     -app.js
   package.json

In above both cases you need to run npm commands inside of project directory because it should be run from path where your package.json exist not app.js.

Well, I ran it in the V2_starter_files_callbacks folder's app.js, and the package.json file is inside the same folder, but should I run it from package.json then?

As per the log you're running command in

C:\Users\babyoscar\

It should be in project directory. Can you confirm the path?

Oh, I think I get it. I have to run cd directoryName before that right?

Okay, now it throws a different error, it says:

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! async-express@1.0.0 start: `nodemon`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the async-express@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\babyoscar\AppData\Roaming\npm-cache\_logs\2020-12-20T21_14_16_744Z-debug.log

Have you installed all dependencies. If yes, than follow Nghia Nguyen comment.

Okay, thanks.