タグ : Node.js

[Yeoman] You don’t seem to have a generator with the name chrome-extension installed.

nvm + Yeoman という環境で generator が見つからないというエラーに陥りました。

➜  yo chrome-extension
 
Error chrome-extension 
 
You don't seem to have a generator with the name chrome-extension installed.
You can see available generators with npm search yeoman-generator and then install them with npm install [name].
To see the 0 registered generators run yo with the `--help` option.

yo –help してみてもインストールしたはずの generator が何も表示されない・・・。

➜  yo --help
Usage: yo GENERATOR [args] [options]
 
General options:
  -h, --help     # Print generator's options and usage
  -f, --force    # Overwrite files that already exist
 
Please choose a generator below.

自分のPC環境の Node.js のデフォルトバージョンが v0.10.26 で、今回 Yeoman をインストールしていたのが v0.10.29 で、環境変数 $NODE_PATH の設定が切り替わってないのが原因でした。

echo $NODE_PATH
/Users/your_username/.nvm/v0.10.26/lib/node_modules
 
➜  echo $NVM_PATH
/Users/your_username/.nvm/v0.10.29/lib/node

環境変数 NODE_PATH を NVM_PATH を使って、再設定すれば解決です。

export NODE_PATH=${NVM_PATH}_modules
➜  echo $NODE_PATH
/Users/your_username/.nvm/v0.10.29/lib/node_modules

でも、nvm use で切替えたときに、毎回 NODE_PATH をセットするのは正直メンドイ。

nvm use で一緒に NODE_PATH も切り替えたい。

というわけで、.zshrc に export_node_path と nvmuse 関数を定義しました。

## nvm
if [[ -f ~/.nvm/nvm.sh ]]; then
  source ~/.nvm/nvm.sh
 
  if which nvm >/dev/null 2>&1 ;then
    export_node_path () {
      export NODE_PATH=${NVM_PATH}_modules
    }
 
    nvmuse () {
      nvm use $1
      export_node_path
    }
 
    export_node_path
  fi
fi

これで nvmuse コマンドを使って、Node.js のバージョン切替えと、環境変数 NODE_PATH を一緒に切替えられるようになりました。

➜  ~  node -v
v0.10.26
➜  ~  echo $NODE_PATH
/Users/your_username/.nvm/v0.10.26/lib/node_modules
 
➜  ~  nvmuse v0.10.29
Now using node v0.10.29
➜  ~  echo $NODE_PATH
/Users/your_username/.nvm/v0.10.29/lib/node_modules

もっと良い方法ありそうだけど、とりあえずこれで良いかな。


参考情報

Yo doesn't install any generators (nvm, node 0.10.24), yo 1.0.7-pre2 · Issue #125 · yeoman/yo

[Node.js] node-wkhtmltopdf を使う

Node.js で wkhtmltopdf を使う方法をメモ。
(最終更新日:2014/11/28)

node-wkhtmltopdf

A Node.js wrapper for the wkhtmltopdf command line tool.

wkhtmltopdf

ということで、npm module だけだと使えないので wkhtmltopdf はインストールする必要があります。

wkhtmltopdf のインストール

# the wkhtmltopdf formula is now inactive but still available in the boneyard repository
brew tap homebrew/boneyard
 
# Install wkhtmltopdf
brew install wkhtmltopdf

wkhtmltopdf コマンドで、PDFを生成できることを確認します。

wkhtmltopdf "http://google.com" google.pdf

以上です。

[Ghost] ブログテーマ (theme) の変更方法

Ghost のブログテーマを変更しました。

Ghostのテーマを作成するには – Ghost日本語ガイド

Ghost Marketplace に無料のテーマがいくつかあるので、この中から選びました。

Free Archives » Ghost Marketplace

Scriptor という Theme を使いたいと思います。

Scriptor – A Beautiful, Simple & Free Ghost Theme for Writers | Just Good Themes

ダウンロードしてきて、Zipファイルを解凍して、content/themes 以下へ移動します。

mv ~/Downloads/scriptor ./content/themes

これだけで、Themeの設置は完了です。

あとは、Ghostのウェブ管理画面 http://127.0.0.1:2368/ghost/settings/general/ から、Themeを選択してSaveするだけでOKです。

ghost-theme-setting

以上です。

Node.js 製ブログプラットフォーム「Ghost」をHerokuへデプロイする

新しくブログを始めたくなって、折角なので Node.js 製ブログプラットフォーム「Ghost」を使うことにしました。

まず、開発環境で起動する手順は、公式ドキュメント(日本語)だけでサクッとできました。

Ghostをインストールするには – Ghost日本語ガイド

次に、Ghost を Heroku へデプロイする手順ですが、こちらも手順記事がいくつか転がっていたので下記3つを参考にしてやりました。

How to run Ghost on Heroku

How To Install Ghost On Heroku

Node.js – Ghostで作ったブログをherokuに公開する – Qiita

とりあえず、Heroku に公開するという最低限の準備ができたので、これから色々と触っていきたいと思います。

[Node.js] ファイルの拡張子を取得する path.extname

Node.js の path module を使って、下記のようにファイルの拡張子を取得することができます。

path.extname('index.html')
// returns
'.html'
 
path.extname('index.coffee.md')
// returns
'.md'
 
path.extname('index.')
// returns
'.'
 
path.extname('index')
// returns
''

参考情報

Path Node.js v0.10.26 Manual & Documentation

[Node.js] RangeError: Maximum call stack size exceeded

Node.js で、RangeError: Maximum call stack size exceeded エラーが発生したときの対応方法をメモ。

% node -v
v0.10.26
 
% node --v8-options | grep -B0 -A1 stack_size
  --stack_size (default size of stack region v8 is allowed to use (in kBytes))
        type: int  default: 984

Node.js version 0.10.26 は、デフォルトの stack size は 984 KB です。

% node -h 
...
 
Options:
  --max-stack-size=val set max v8 stack size (bytes)
 
...

ヘルプには –max-stack-size で指定すると書いてありますが、

node --max-stack-size=val

v0.10.x 以上だと

node --stack-size=val

–stack-size で指定するっぽいです。


参考情報

What is the default stack size in Node.js? – Stack Overflow

javascript – How can I increase the maximum call stack size in Node.js – Stack Overflow

[Node.js] ファイルの存在チェック

Node.js で、viewファイルの存在チェックして、無ければリダイレクトさせる処理をメモ。

var fs = require('fs');
 
var template = 'novels/'+ title + '.jade';
var templateFilePath = 'views/' + template;
 
fs.stat(templateFilePath, function(e) {
  if (e) {
    console.error(templateFilePath + " file does't exist.");
    return res.redirect('/novels');
  }
 
  return res.render(template);
});

ここでは、テンプレートエンジンは jade を使っているという前提で、動的に生成したファイル名に対応する jade ファイルが存在するかチェックしています。

[Node.js][node-csv] Error: Invalid closing quote at line 1; found ” ” instead of delimiter “\t”

Node.js で使える CSV Parser node-csv でタブ区切りテキストの中にダブルクオーテーション ” を含めていたら下記のようなエラーが発生しました。

wdavidw/node-csv

Error: Invalid closing quote at line 1; found " " instead of delimiter "\t"
  at [object Object].Parser.write (/u/apps/com/shared/node_modules/csv/lib/parser.js:104:29)
  at [object Object].CSV.write (/u/apps/com/shared/node_modules/csv/lib/index.js:275:17)
  at write (_stream_readable.js:583:24)
  at flow (_stream_readable.js:592:7)
  at ReadStream.pipeOnReadable (_stream_readable.js:624:5)
  at ReadStream.EventEmitter.emit (events.js:92:17)
  at emitReadable_ (_stream_readable.js:408:10)
  at emitReadable (_stream_readable.js:404:5)
  at readableAddChunk (_stream_readable.js:165:9)
  at ReadStream.Readable.push (_stream_readable.js:127:10)
  at onread (fs.js:1561:12)
  at Object.wrapper [as oncomplete] (fs.js:454:17)

一部、手動で作成したCSVファイルだったので、ダブルクオーテーションを消して解決しました。


参考情報

double quotes in a tab-delimited file throws error · Issue #46 · wdavidw/node-csv

[Express.js] Error: Request Entity Too Large の解決方法

Express.js で大きいサイズのファイルを POST したら Request Entity Too Large というエラーが発生して、解決した内容をご紹介します。

Express.js

続きを読む

[Mac] Node.js と npm をインストールする方法 [Homebrew]

Macに Node.js と npm をインストールする方法を調べたのでメモ。

node.js を homebrew でインストールする

$ brew install node

正常にインストールされたことを確認します。

$ node -v
v0.4.11

 

npm を homebrew でインストールできない…

Homebrew の npm formula は、収録のポリシーに沿わない( https://github.com/mxcl/homebrew/wiki/Acceptable-Formula )として最近、取り除かれたそうです。

メッセージにあるように curl http://npmjs.org/install.sh | sh でインストールすべきみたいです。

$ brew install npm
Error: No available formula for npm
npm can be installed thusly by following the instructions at
  http://npmjs.org/
 
To do it in one line, use this command:
  curl http://npmjs.org/install.sh | sh

 

npm を curl経由でインストールする

$ curl http://npmjs.org/install.sh | sh

正常にインストールされたことを確認します。

$ npm -v
1.0.103

以上です。


参考情報

node.jsとnpmのインストール – 自分の感受性くらい

MacOSXでnode.js/npm/nvmをインストール – tagomorisのメモ置き場