Node.js Express導入してうごくまで 2016

開発環境:私物MacBookPro

$ npm install express
昔はこれでexpressコマンドが使えたらしいのだが、最近になって別にコマンドを打つ必要が増えたそうだ。
そこで以下を実行。

$ sudo npm install -g express-generator
$ which express
/usr/local/bin/express

参考:http://www.atmarkit.co.jp/ait/articles/1503/04/news047_3.html

これでexpressアプリがつくれるようになる。

はずだった・・・

$ express myexapp

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

   install dependencies:
     $ cd myexapp && npm install

   run the app:
     $ DEBUG=myexapp:* npm start

$ ls
index.ejs  index.jade index.js   myexapp    server.js  style.css  test.jade  test.js
$ cd myexapp/
$ ls
app.js       bin          package.json public       routes       views
[hyperpoemcreator@takenouchi-no-MacBook-Pro myexapp]$ node app.js

module.js:333
    throw err;
          ^
Error: Cannot find module 'serve-favicon'
    at Function.Module._resolveFilename (module.js:331:15)
    at Function.Module._load (module.js:273:25)
    at Module.require (module.js:357:17)
    at require (module.js:373:17)
    at Object.<anonymous> (/Users/hyperpoemcreator/dev/node/myexapp/app.js:3:15)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:349:32)
    at Function.Module._load (module.js:305:12)
    at Function.Module.runMain (module.js:490:10)

はわわふぁわ!!!!!

$ sudo npm install npm-install-missing -g
npm http GET https://registry.npmjs.org/npm-install-missing
npm http 200 https://registry.npmjs.org/npm-install-missing
npm http GET https://registry.npmjs.org/npm-install-missing/-/npm-install-missing-0.1.4.tgz
npm http 200 https://registry.npmjs.org/npm-install-missing/-/npm-install-missing-0.1.4.tgz
npm http GET https://registry.npmjs.org/npm
npm http GET https://registry.npmjs.org/async
npm http 200 https://registry.npmjs.org/async
npm http GET https://registry.npmjs.org/async/-/async-1.5.2.tgz
npm http 200 https://registry.npmjs.org/async/-/async-1.5.2.tgz
npm http 200 https://registry.npmjs.org/npm
npm http GET https://registry.npmjs.org/npm/-/npm-3.7.2.tgz
npm http 200 https://registry.npmjs.org/npm/-/npm-3.7.2.tgz
/usr/local/bin/npm-install-missing -> /usr/local/lib/node_modules/npm-install-missing/bin/npm-install-missing
npm-install-missing@0.1.4 /usr/local/lib/node_modules/npm-install-missing
├── async@1.5.2
└── npm@3.7.2

参考:http://d.hatena.ne.jp/replication/20150214/1423909149

では

$ node app.js

module.js:333
    throw err;
          ^
Error: Cannot find module 'serve-favicon'
    at Function.Module._resolveFilename (module.js:331:15)
    at Function.Module._load (module.js:273:25)
    at Module.require (module.js:357:17)
    at require (module.js:373:17)
    at Object.<anonymous> (/Users/hyperpoemcreator/dev/node/myexapp/app.js:3:15)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:349:32)
    at Function.Module._load (module.js:305:12)
    at Function.Module.runMain (module.js:490:10)
$

うおおおおお

$ sudo npm-install-missing
Package        Current  Wanted  Latest  Location
body-parser    MISSING  1.13.3  1.15.0  myexapp
cookie-parser  MISSING   1.3.5   1.4.1  myexapp
debug          MISSING   2.2.0   2.2.0  myexapp
express        MISSING  4.13.4  4.13.4  myexapp
jade           MISSING  1.11.0  1.11.0  myexapp
morgan         MISSING   1.6.1   1.6.1  myexapp
serve-favicon  MISSING   2.3.0   2.3.0  myexapp
[ [ 'body-parser', '1.13.3' ],
  [ 'cookie-parser', '1.3.5' ],
  [ 'debug', '2.2.0' ],
  [ 'express', '4.13.4' ],
  [ 'jade', '1.11.0' ],
  [ 'morgan', '1.6.1' ],
  [ 'serve-favicon', '2.3.0' ] ]
myexapp@0.0.0 /Users/hyperpoemcreator/dev/node/myexapp
└── debug@2.2.0

myexapp@0.0.0 /Users/hyperpoemcreator/dev/node/myexapp
└── morgan@1.6.1

myexapp@0.0.0 /Users/hyperpoemcreator/dev/node/myexapp
└── express@4.13.4

myexapp@0.0.0 /Users/hyperpoemcreator/dev/node/myexapp
└── body-parser@1.13.3

myexapp@0.0.0 /Users/hyperpoemcreator/dev/node/myexapp
└── cookie-parser@1.3.5

myexapp@0.0.0 /Users/hyperpoemcreator/dev/node/myexapp
└── serve-favicon@2.3.0

myexapp@0.0.0 /Users/hyperpoemcreator/dev/node/myexapp
└── jade@1.11.0

npm-install-missing: Successfully installed: body-parser@1.13.3, cookie-parser@1.3.5, debug@2.2.0, express@4.13.4, jade@1.11.0, morgan@1.6.1, serve-favicon@2.3.0
$

うほなんかいけそう。

$ node app.js
$

ほよ?

ggrと以下記事がhit

Unable to run node app.js file
http://stackoverflow.com/questions/23178363/unable-to-run-node-app-js-file

$ vi package.json
{
  "name": "myexapp",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.13.2",
    "cookie-parser": "~1.3.5",
    "debug": "~2.2.0",
    "express": "~4.13.1",
    "jade": "~1.11.0",
    "morgan": "~1.6.1",
    "serve-favicon": "~2.3.0"
  }
}

ふむふむ

$  node bin/www
GET / 200 1823.576 ms - 170
GET /stylesheets/style.css 200 4.670 ms - 111
GET /favicon.ico 404 45.035 ms - 11251

スクリーンショット 2016-02-17 22.57.48

うごいたーーーーーーーーーーーーーーーーーーー

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です