5

So I'm following This Tutorial here to get a personal/learning node.js project off the ground that I would like to work on and store in GitHub and eventually put onto a live server or cloud hosting. It's using node.js, express, monk, mongoDB for a website setup. However I have a persistent error that I haven't found an answer to when I run 'npm start'.

Here is my command window output when I try to run 'npm start' inside my node express project.

I know that: Error: Cannot find module 'C:\Users\username\Desktop\bin\www' is not where my bin\www is located (it's inside the nodetest1 project).

C:\Users\username\Documents\GitHub\TEDResources\node.js\nodetest1 <-- correct path

So, why is npm looking at that incorrect spot and how do I update it to correctly look at my project folder? I've searched my system and can't find module.js anywhere. (using windows 7 x64) I've also tried nuking node.js and express and started from scratch without luck.

//console error output

Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\username\Documents\GitHub\TEDResources\node.js\nodetest1>npm start

> nodetest1@0.0.0 start C:\Users\username\Documents\GitHub\TEDResources\node.j
s\nodetest1

> node ./bin/www
module.js:340
    throw err;
          ^
Error: Cannot find module 'C:\Users\username\Desktop\bin\www'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

npm ERR! nodetest1@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 8
npm ERR!
npm ERR! Failed at the nodetest1@0.0.0 start script.
npm ERR! This is most likely a problem with the nodetest1 package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./bin/www
npm ERR! You can get their info via:
npm ERR!     npm owner ls nodetest1
npm ERR! There is likely additional logging output above.
npm ERR! System Windows_NT 6.1.7601
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nod
ejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! cwd C:\Users\username\Documents\GitHub\TEDResources\node.js\nodetest1

npm ERR! node -v v0.10.32
npm ERR! npm -v 1.4.28
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     C:\Users\username\Documents\GitHub\TEDResources\node.js\nodetest1
\npm-debug.log
npm ERR! not ok code 0

//npm-debug.log

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'start' ]
2 info using npm@1.4.28
3 info using node@v0.10.32
4 verbose node symlink C:\Program Files\nodejs\\node.exe
5 verbose run-script [ 'prestart', 'start', 'poststart' ]
6 info prestart nodetest1@0.0.0
7 info start nodetest1@0.0.0
8 verbose unsafe-perm in lifecycle true
9 info nodetest1@0.0.0 Failed to exec start script
10 error nodetest1@0.0.0 start: `node ./bin/www`
10 error Exit status 8
11 error Failed at the nodetest1@0.0.0 start script.
11 error This is most likely a problem with the nodetest1 package,
11 error not with npm itself.
11 error Tell the author that this fails on your system:
11 error     node ./bin/www
11 error You can get their info via:
11 error     npm owner ls nodetest1
11 error There is likely additional logging output above.
12 error System Windows_NT 6.1.7601
13 error command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
14 error cwd C:\Users\kellington\Documents\GitHub\TEDResources\node.js\nodetest1
15 error node -v v0.10.32
16 error npm -v 1.4.28
17 error code ELIFECYCLE
18 verbose exit [ 1, true ]

//package.json

{
  "name": "nodetest1",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "express": "~4.9.0",
    "body-parser": "~1.8.1",
    "cookie-parser": "~1.3.3",
    "morgan": "~1.3.0",
    "serve-favicon": "~2.1.3",
    "debug": "~2.0.0",
    "jade": "~1.6.0", 
    "mongodb": "*",
    "monk": "*"
  }
}

//app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});


module.exports = app;

Microsoft Windows [Version 6.1.7601]

Copyright (c) 2009 Microsoft Corporation. All rights reserved.

//dir result

> nodetest1@0.0.0 start C:\Users\username\Documents\GitHub\TEDResources\node.j
s\nodetest1
> dir

 Volume in drive C is OS
 Volume Serial Number is F87D-8771

 Directory of C:\Users\username\Desktop

10/23/2014  11:13 AM    <DIR>          .
10/23/2014  11:13 AM    <DIR>          ..
10/13/2014  09:14 AM           146,016 7f6bd778-1c67-43fd-9865-372789ed7592_Loca
lIISWebsite.zip
10/07/2014  03:39 PM             1,086 Blue Eye Macro.lnk
08/28/2014  12:07 PM             1,197 DesignEphiphany.txt
10/22/2014  08:03 AM             2,163 Git Shell.lnk
04/17/2014  10:51 AM               308 GitHub.appref-ms
10/21/2014  10:54 AM    <DIR>          LocalIISWebsite
10/23/2014  11:11 AM    <DIR>          node.js
10/14/2014  10:07 AM    <DIR>          TextDocs

               7 File(s)      1,704,717 bytes
               8 Dir(s)  352,881,205,248 bytes free
subpath42
  • 81
  • 1
  • 1
  • 9
  • Try changing it to `"start": "node bin/www"` in your package.json. Also, for debugging purposes, try putting a `"dir": "dir"` right below it (don't forget to add a comma above for syntax, it's basically JSON) and run `npm dir` to show which directory it's running from. – laggingreflex Oct 17 '14 at 16:44
  • I tried updating the "start" in the package but it gives the same error. For the 'npm dir' command do I need to install another module? It just spits out the npm command help. – subpath42 Oct 21 '14 at 15:53
  • First, here's how `npm` `package.json`'s `"script"` element works: you put in it an `"alias": "command"` which executes the "command" when you run `npm alias`. This is why I suggested you put `"dir": "dir"` and run `npm dir`. If that gives an error you're probably not on windows as `dir` is windows command. Replace it with `pwd` which is the bash command to look up current directory. The reason for this debugging is so that you could know exactly which directory is the `npm` being executed from. – laggingreflex Oct 22 '14 at 06:05
  • Sorry I wasn't clear, I added the "dir": "dir" as instructed and ran 'npm dir' but received the "User: npm where etc...the last line is npm@1.4.28 c:\Program Files\nodejs\node_modules\npm <---is that what I am looking for? – subpath42 Oct 22 '14 at 14:14
  • I have to apologize, I wasn't right about how npm works that way. Disregard the `"dir":"dir"` part I said above. Instead, change the start to `"start":"dir"` and run `npm start`. That will surely work. Again, extremely sorry for the mis-information :} – laggingreflex Oct 23 '14 at 03:46
  • Alright, did that, directory came up as 'C:\Users\username\Desktop', but I don't have npm installed there. I have a node.js folder there that I made for some tests that used IO/mysql modules and some hand written scripts but not npm or any of the modules currently in use for this project. I'll add the output above. – subpath42 Oct 23 '14 at 17:15
  • Then you must be running `npm start` from the desktop. You should be running from within your application directory. Do a `cd C:\Users\username\Documents\GitHub\TEDResources\node.js\nodetest1` and then run it. – laggingreflex Oct 23 '14 at 17:20
  • That's the thing that is confusing me, I do execute npm start from that directory, it just seems like I'm missing a config somewhere in npm that defaulted it to the desktop instead of the correct directory. – subpath42 Oct 23 '14 at 17:22
  • A workaround would be to change it to `"start": "cd C:\\Users\\username\\Documents\\GitHub\\TEDResources\\node.js\\nodetest1 & node ./bin/www"`. – laggingreflex Oct 23 '14 at 17:28
  • ...alright, I am truly an idiot. I adjusted my registry to make a default start dir for cmd and apparently that included overriding npm and my own commands. Thank you for your assistance in the laggingreflex. – subpath42 Oct 23 '14 at 17:37

7 Answers7

10

I had this same error when I was creating my First Node Project using express. But when i tried running it using

node ./bin/www              // it will work

instead of

npm start

The server started at port 3000. you can check your app on localhost:3000 .
May be it can help some One else :)

farhangdon
  • 2,003
  • 2
  • 21
  • 30
  • That works!!!! what might be the problem to npm start. I mean why its not working and node ./bin/www works??? – Vishal Jun 27 '16 at 20:54
4

Fwiw, I had the same error and it turned out to be that something was already running on port 3000. I mention this because the debug log mentioned nothing about this, just a similar error to yours. So maybe this will help somebody who comes across this post and happens to have the problem I did.

Dave Munger
  • 479
  • 3
  • 11
2

Had a windows registry entry for "autorun" - "problem directory" setup for a default start directory that was overriding commands. Removed it from registry and fixed the issue. Thank you to laggingreflex for helping me debug this.

subpath42
  • 81
  • 1
  • 1
  • 9
  • not horribly detailed...where did you find the registry key? – Chaim Eliyah Feb 25 '15 at 21:29
  • 1
    I had setup an autorun in the regedit under HKEY_CURRENT_USER\Software\Microsoft\Command Processor which had its value set to the directory that was causing my problem. I had originally set it up for convenience so that the command prompt would be looking at my default project directory but it looks like using a startup script to do this is the better way. http://stackoverflow.com/questions/4895966/changing-default-startup-directory-for-command-prompt-in-windows-7 – subpath42 Feb 26 '15 at 14:59
1

I had the same problem following this tutorial, only after killing server for the first time as per instructions. I tried a bunch of things, including those listed above and on several other threads. However simply removing or commenting out the following in app.js did the trick :

//var mongo = require('mongodb')
lacostenycoder
  • 10,623
  • 4
  • 31
  • 48
0

This happened to me today, same error looking to the same tutorial, but with this setup:

  • Ubuntu 14.04
  • npm v5.11.1
  • node v3.9.2

First intent was searching on the web and found this page, and can't solve the problem with what was said, but found a clue to follow.

Second intent was to list apps using the same port (3000) with this:

sudo netstat -ep | grep 3000

But nothing.

Third intent was to change the port number used un "nodetest1/bin/www" file from 3000 to 3001, and that did the trick.

var port = normalizePort(process.env.PORT || '3001');

From there, I now can browse http://localhost:3001 and keep following the tutorial.

Edit: I've found that I've incorrectly used netstat, and this showed whats program is using the port 3000:

sudo netstat -epa | grep 3000

ntop is using it:

tcp        0      0 *:3000                  *:*                     ESCUCHAR    root       15508       2289/ntop

So in my case changing the nodetest1 port on www file was the only viable solution.

Hope this help others.

Fer B.
  • 425
  • 5
  • 11
0

I had the same error, so i run:

npm install

and then:

npm start

and that works for me

0

In the tutorial's app.js file, be sure to name the index object as follows:

var index = require('./routes/index');

This should correct the error.

Despite what the tutorial author says, the error will throw if that line reads:

var index = require('./routes/routes');
tj_m
  • 1
  • 2