LoginSignup
10
9

More than 5 years have passed since last update.

npm モジュール作りの覚書

Posted at
  • CoffeeScript で開発
  • JavaScript で公開
  • 公開モジュールに CoffeeScript は入れたくない(なんとなく)
  • 自動生成される JavaScript を git 管理したくない
  • test は mocha ✕ chai ✕ sinon

ディレクトリ構成

node_modules/
spec/
.gitignore
.npmignore
index.coffee
index.js
package.json
README.md

その他、ファイルが増える場合は lib ディレクトリや bin ディレクトリなどが増える。

npm install

npm install --save-dev coffee-script               # CS のコンパイル
npm install --save-dev mocha chai sinon sinon-chai # テスト用

.gitignore

.gitignore
**/*.js
node_modules/

.npmignore

.npmignore
**/*.coffee
node_modules/
spec/

package.json

package.json
{
  "name": "{{module name}}",
  "version": "{{version}}",
  "main": "index.js",
  "scripts": {
    "prepublish": "coffee -c index.coffee",
    "test": "env NODE_PATH=. mocha --compilers coffee:coffee-script/register --recursive spec/ -R nyan"
  },
  "中": "略",
  "devDependencies": {
    "chai": "",
    "coffee-script": "",
    "mocha": "",
    "sinon": "",
    "sinon-chai": ""
  }
}

モジュール作成から公開までの流れ

  • npm 公式サイト で自分のアカウントを作る
  • npm adduser で開発者の登録
    • user name, password, email を求められるので、アカウント作った時の情報を入力
    • email は公開されるので注意
  • npm init でモジュール作りまっせ宣言
  • 開発
  • npm publish で公開
    • prepublish によって index.js が作られ、それが公開される
10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9