22

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
Bren
  • 3,516
  • 11
  • 41
  • 73
  • I rand `node app` and it gave the same error. Actually it is error on the `Error: Cannot find module 'static-favicon'`. – Bren Jun 20 '14 at 19:07
  • Which version of Expressjs are you using? – Michael Jun 20 '14 at 19:08
  • 1
    4.2.0 is the version/ – Bren Jun 20 '14 at 19:09
  • Post your `app.js` modules and `app.use`s – Michael Jun 20 '14 at 19:10
  • 1
    it can't 'static-favicon' - you have to require it? or point to it correctly? – Dan Jun 20 '14 at 19:10
  • there seems to be no modules folder actually. I'm just running `express` in an empty directory. – Bren Jun 20 '14 at 19:16
  • Module can mean anything... if you just have an empty directory with nothing but express... and you are trying to access favicon... thats not gonna work... you gotta tell express where it is relative to the app directory... – Dan Jun 20 '14 at 19:31
  • so the app the results from the `express` command doesn't work out of the box? – Bren Jun 20 '14 at 19:34

5 Answers5

37

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
dylants
  • 22,316
  • 3
  • 26
  • 22
  • Well npm can start but not node or forever. However, thats all I need so I'm accepting the answer. I thought express automatically installed the modules and since it didn't there was something wrong. Though I was using it on an app that worked before so I really don't know exactly why this helped with that one. Hmmm. Thanks! – Bren Jun 20 '14 at 19:51
  • When you run `npm start` you're really running `node ./bin/www` (which is what I was trying to say above in my answer). – dylants Jun 20 '14 at 19:53
10

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
Jan Michael Auer
  • 711
  • 9
  • 16
1

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)

Dan
  • 3,755
  • 4
  • 27
  • 38
  • There seems to be no modules folder so, nothing to require to. Shouldn't express create one? The line currently is `var favicon = require('static-favicon');`. – Bren Jun 20 '14 at 19:21
  • where is static-favicon? in the same directory? './thepath' up one directory? '../thepath' – Dan Jun 20 '14 at 19:23
  • Honestly, i'm new to node so I don't know. Is it a local module or a global one? It doesn't seem to be in any of the simple express apps I've made. – Bren Jun 20 '14 at 19:31
  • The node module system is explained here: https://nodejs.org/api/modules.html In your case, when writing `require('static-favicon')` Node it will look for a module of the same name in your `node_modules` folder created when installing node modules. You can list all currently installed modules by invoking `npm list`. – Jan Michael Auer Jan 15 '16 at 14:36
0

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.

0

Do npmstart which installs all the required modules for your project