Program LOG

勉強した内容をまとめ中。。。間違ってたら教えて。。。

CygwinでGruntjsをインストール

前回の記事で、Compass+Sassを行いました!
CygwinでSass+Compassをインストール - Program LOG
そこで、今回はCygwinでGruntjsを使ってみようと思います。
1年位前から色んなブログですでに紹介されており、
ドットインストールでも紹介されていたので、調べるのに苦労はなさそうw
って思ったら、Cygwinでnode使うのにすげーーーーハマッタw

Grunt.jsって?

色んな事を自動化してくれるビルドツールです。
例えば、Webサイトの表示速度を気にすると、CSSJavaScriptのminify、gzipCSS Sprite、
画像の最適化などの面倒な作業が発生します。
この間やった、Compassでsassのコンパイル処理もGruntがやってくれたりします。

なんとなく概要は分かったので、ひとまずインストールしてみたいと思います。
Grunt.jsをインストールするには、nodejsが必要なので、node.jsからインストールしていきます。

Node.jsのインストール

nodeJSのインストーラーは公式サイトからダウンロードできます。

f:id:aimtaku:20140614102616p:plain

「INSTALL」ボタンからダウンロードしたら、
ガイダンスにしたがって、インストールをしちゃいましょう!

インストールが完了したら、「npm」コマンドが実行できるはずなので、
試してみたいと思います。

$ npm -v
/cygdrive/c/Program Files/nodejs/npm: line 2: $'\r': command not found
/cygdrive/c/Program Files/nodejs/npm: line 4: $'\r': command not found
/cygdrive/c/Program Files/nodejs/npm: line 5: syntax error near unexpected token `$'in\r''
'cygdrive/c/Program Files/nodejs/npm: line 5: `case `uname` in

ん?・・・エラーw改行コード?w
ちょっと調べたら、似た現象の人を発見しました!
Node.jsをWindowsインストーラで入れたら、Cygwinのシェルでnpmがエラーになる - 死ぬまでの暇潰し
ありがたかった・・・w改行コードをCRLFからLFに変更して保存しなおします。

$ npm -v
cygwin warning:
  MS-DOS style path detected: C:\Program Files\nodejs/node.exe
  Preferred POSIX equivalent is: /cygdrive/c/Program Files/nodejs/node.exe
  CYGWIN environment variable option "nodosfilewarning" turns off this warning.
  Consult the user's guide for more details about POSIX paths:
    http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
1.4.9

おー!案外簡単にいけました!

Grunt.jsのインストール

先ほどインストールしたNode.jsからインストールします。

$ npm install -g grunt-cli
npm http GET https://registry.npmjs.org/grunt-cli
npm http 200 https://registry.npmjs.org/grunt-cli
npm http GET https://registry.npmjs.org/grunt-cli/-/grunt-cli-0.1.13.tgz
npm http 200 https://registry.npmjs.org/grunt-cli/-/grunt-cli-0.1.13.tgz
npm http GET https://registry.npmjs.org/nopt
npm http GET https://registry.npmjs.org/findup-sync
npm http GET https://registry.npmjs.org/resolve
npm http 200 https://registry.npmjs.org/findup-sync
・・・中略・・・
npm http GET https://registry.npmjs.org/lru-cache/-/lru-cache-2.5.0.tgz
npm http 200 https://registry.npmjs.org/sigmund
npm http 200 https://registry.npmjs.org/lru-cache/-/lru-cache-2.5.0.tgz
npm http GET https://registry.npmjs.org/sigmund/-/sigmund-1.0.0.tgz
npm http 200 https://registry.npmjs.org/sigmund/-/sigmund-1.0.0.tgz
C:\Users\takumi-main\AppData\Roaming\npm\grunt -> C:\Users\takumi-main\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt
grunt-cli@0.1.13 C:\Users\・・・\AppData\Roaming\npm\node_modules\grunt-cli
├── resolve@0.3.1
├── nopt@1.0.10 (abbrev@1.0.5)
└── findup-sync@0.1.3 (lodash@2.4.1, glob@3.2.11)

イケタっぽい?
gruntを実行します。

$ grunt
grunt-cli: The grunt command line interface. (v0.1.13)

ん?・・・これでいいのか?w色んなサイトを見るとコレでいいらしい・・w
・・・おーなるほど!
grunt.jsは本体もプラグイン扱いらしく、package.jsonで本体をインストールするらしいw

package.jsonを作成する。

grantを使用するには、package.jsonというファイルを作成しなければなりません。
これはプロジェクトを管理する為のメタファイルになります。
こちらは、プロジェクトのディレクトリにアクセスして、
下記のコマンドを実施する事によって作成してくれます。

$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install  --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.

コマンドを実施したとしても、作成してくれません。。。
・・・ってか、対話形式でプロジェクトについて聞かれるはずなのに、
何も聞いてくれない・・・・何だこれw
package.jsonのかわりに、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   'init' ]
2 info using npm@1.4.9
3 info using node@v0.10.28
4 error Error: EINVAL, invalid argument
4 error     at new Socket (net.js:156:18)
4 error     at process.stdin (node.js:664:19)
4 error     at read (C:\Program Files\nodejs\node_modules\npm\node_modules\read\lib\read.js:18:36)
4 error     at PromZard.prompt (C:\Program Files\nodejs\node_modules\npm\node_modules\init-package-json\node_modules\promzard\promzard.js:214:3)
4 error     at PromZard.L (C:\Program Files\nodejs\node_modules\npm\node_modules\init-package-json\node_modules\promzard\promzard.js:177:21)
4 error     at PromZard.walk (C:\Program Files\nodejs\node_modules\npm\node_modules\init-package-json\node_modules\promzard\promzard.js:146:5)
4 error     at PromZard.loaded (C:\Program Files\nodejs\node_modules\npm\node_modules\init-package-json\node_modules\promzard\promzard.js:90:8)
4 error     at PromZard. (C:\Program Files\nodejs\node_modules\npm\node_modules\init-package-json\node_modules\promzard\promzard.js:57:10)
4 error     at fs.js:266:14
4 error     at C:\Program Files\nodejs\node_modules\npm\node_modules\graceful-fs\graceful-fs.js:105:5
5 error If you need help, you may report this *entire* log,
5 error including the npm and node versions, at:
5 error     http://github.com/npm/npm/issues
6 error System Windows_NT 6.1.7601
7 error command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "init"
8 error cwd C:\Users\takumi-main\Desktop\dev\grunt
9 error node -v v0.10.28
10 error npm -v 1.4.9
11 error syscall uv_pipe_open
12 error code EINVAL
13 error errno 18
14 verbose exit [ 18, true ]

よくわからん・・・・w色々ググってみる・・・。
日本のサイトにいいのが無い・・・海外では似た現象があったみたいだが・・・
ちなみに、「node」だけで「enter」しても以下のようになる。

$ node

net.js:156
    this._handle.open(options.fd);
                 ^
Error: EINVAL, invalid argument
    at new Socket (net.js:156:18)
    at process.stdin (node.js:664:19)
    at startup (node.js:145:16)
    at node.js:906:3

何か、Socketが上手くいってないっぽいっすな!ちょっと前進?
色々調べたら、Windowsでnodeは出来るけど、Cygwinには対応してないよー!って、きついお言葉が・・・orz
しょうがない・・・アンインストールして、仮想マシンに入れるか・・・?
それともwindows版をそのまま使うか・・・?悩むw

ちょっと寄り道してnodistってバージョン管理ツールを使ってみる

色んなバージョンで試してみようって思って、バージョン管理を探してみたら、あったw
下記コマンドで、クローンして使います。

$ git clone git://github.com/marcelklehr/nodist.git

パスを通して、以下のコマンドを、コマンドプロンプトで実行。

$ nodist -v
0.4.8

インストールできてますね!
次に、アップデートしておきます。

$ nodist update

色々走った上で、nodejsもインストールされます。
最新の安定版node.jsをインストールするには、以下。

$ nodist stable

バージョンを指定する場合は、以下のようにします。

$ nodist + v0.11.11
0.11.11
$ node -v
v0.10.29
$ nodist ls
> 0.10.29
   0.11.11

おー入ったっぽい!それでは、先ほどのが解決しているっていう噂の、
0.11バージョンを指定していみたいと思います。

$ nodist (use) v0.11.11
Version spec didn't match any available version.
Sorry.

・・・・・・・・・・・チーン。
オワタ・・・・wもう折れましたw
正直折れたぞー!w
って事で、もうCygwinでnodeはやらない!これでfix!w
じゃあどうするって感じなんですが、、、
ためしに、package.jsonを自作したら、それでいけちゃったので、
取り合えずnpm initはスルーする方向で・・・w

久々に未解決のままになってしまいましたが、今回は無理だった!って事でご容赦を!
ってかだーれーかー解決方法しってたらおせーてー!w

かなり折れてしまったので、今回は、ここまでで・・・
次回、Gruntの操作をしてみます。

使用環境

  • ホストOS: Windows7 x64
  • Cygwin : Cygwin64
  • ruby : 2.1.2p95
  • VirtualBox: 4.3.10
  • Vagrant: 1.5.2