Bren
Bren

Reputation: 3686

node . and npm start don't work

The title says most of the problem. When I try to run node . I get:

    module.js:340
    throw err;
          ^
Error: Cannot find module 'static-favicon'
    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:902:3

There seems to be no modules folder actually. I'm just running express in an empty directory

npm works fine however. This is with a fresh express install if it matters. Any help would be awesome, thanks!

the full error messages:

 new-host-2:~ Brennan$ cd Desktop/
new-host-2:Desktop Brennan$ mkdir test4
new-host-2:Desktop Brennan$ cd test4
new-host-2:test4 Brennan$ express -e

   create : .
   create : ./package.json
   create : ./app.js
   create : ./public
   create : ./public/javascripts
   create : ./public/images
   create : ./public/stylesheets
   create : ./public/stylesheets/style.css
   create : ./routes
   create : ./routes/index.js
   create : ./routes/users.js
   create : ./views
   create : ./views/index.ejs
   create : ./views/error.ejs
   create : ./bin
   create : ./bin/www

   install dependencies:
     $ cd . && npm install

   run the app:
     $ DEBUG=test4 ./bin/www

new-host-2:test4 Brennan$ node app.js

module.js:340
    throw err;
          ^
Error: Cannot find module 'static-favicon'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/Users/Brennan/Desktop/test4/app.js:3:15)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
new-host-2:test4 Brennan$ npm start app.js
npm ERR! Error: ENOENT, open '/Users/Brennan/Desktop/test4/node_modules/app.js/package.json'
npm ERR! If you need help, you may report this *entire* log,
npm ERR! including the npm and node versions, at:
npm ERR!     <http://github.com/npm/npm/issues>

npm ERR! System Darwin 12.4.0
npm ERR! command "node" "/usr/local/bin/npm" "start" "app.js"
npm ERR! cwd /Users/Brennan/Desktop/test4
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.7
npm ERR! path /Users/Brennan/Desktop/test4/node_modules/app.js/package.json
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/Brennan/Desktop/test4/npm-debug.log
npm ERR! not ok code 0
new-host-2:test4 Brennan$ forever app.js
warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms

module.js:340
    throw err;
          ^
Error: Cannot find module 'static-favicon'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/Users/Brennan/Desktop/test4/app.js:3:15)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
error: Forever detected script exited with code: 8

Upvotes: 22

Views: 35429

Answers (5)

Jan Michael Auer
Jan Michael Auer

Reputation: 731

I had the same issue after running the express-generator on a non-empty directory with some node modules already installed in node_modules, especially express itself. Simply nuke the folder, reinstall all your dependencies and you should be good to go:

rm -rf ./node_modules
npm install
npm start

EDIT: Tuns out that at some stage in the process I had installed serve-favicon and saved that to the local package.json. Looks like the express generator failed to add that dependency. Therefore:

npm install serve-favicon --save
npm start

Upvotes: 10

santhosh
santhosh

Reputation: 1

Do npmstart which installs all the required modules for your project

Upvotes: 0

user4447835
user4447835

Reputation: 51

Use serve-favicon instead of static-favicon. Also verify that you have serve-favicon installed correctly. Do cd serve-favicon, check package.json, and then run npm install to have all dependencies installed correctly.

Upvotes: 0

dylants
dylants

Reputation: 23340

After using the express-generator to generate the node application, you need to install the dependencies for the project. This is done via:

$ npm install

Once this is done, you can start the app using npm:

$ npm start

By default, the express generated apps state this as the start command for npm (you can view this in the package.json file):

"start": "node ./bin/www"

So to execute the same thing via the command line, you would run:

$ node ./bin/www

Upvotes: 37

Dan
Dan

Reputation: 3815

given a dir tree like:

myapp
 - lib
  - favicon.ico
 - app.js
 - package.json
 - node_modules (npm installs all modules in here)

if you are trying to access favicon.ico in your app.js

var express = require('express'),
    fs = require('fs'); // this is for reading static files 

var favicon = require(fs.readFileSync('./lib/favicon.ico'));

the var for favicon may not be what you expect. The best way for serving static files would be to set up a static directory with a route so anyone hitting that route with a param gets that files: for example:

https://mynodeapp.com/static/images/{param}

where param would be the name of the file (this is an example using Hapi - express has something similiar for serving static files)

Upvotes: 1

Related Questions