LoginSignup
20

More than 5 years have passed since last update.

CoffeeScriptでnpmモジュールを書く

Posted at

CoffeeScriptでnpmモジュールを書いたりしたことなかった(し、これからも多分しない)ので、もし書くとしたらこんな感じかなーとやってみたら出来たのでメモメモ。

モジュールを作る

とりあえずpackage.jsonを書く。

package.json
{
  "name": "test",
  "version": "0.0.0",
  "private": true,
  "main": "./index.js",
  "scripts": {
    "postinstall": "coffee -bc index.coffee"
  },
  "dependencies": {
    "coffee-script": "1.7.1"
  }
}

scriptspostinstallがキモかなー。

スクリプトを書く

とりあえずindex.coffeeを書く。

index.coffee
module.exports = -> console.log 'Hello, World!'

インストールしてみる

ディレクトリが現在こんな感じ。

|-- coffee/
|   |-- index.coffee
|   |-- package.json
|-- js/

で、カレントディレクトリがjs/の状態から。

Terminal
$ npm i ../coffee
npm http GET https://registry.npmjs.org/coffee-script/1.7.1
npm http 200 https://registry.npmjs.org/coffee-script/1.7.1
npm http GET https://registry.npmjs.org/coffee-script/-/coffee-script-1.7.1.tgz
npm http 200 https://registry.npmjs.org/coffee-script/-/coffee-script-1.7.1.tgz
npm http GET https://registry.npmjs.org/mkdirp
npm http 304 https://registry.npmjs.org/mkdirp

> test@0.0.0 postinstall /Users/macbookpro/Work/js/node_modules/test
> coffee -bc index.coffee

test@0.0.0 node_modules/test
└── coffee-script@1.7.1 (mkdirp@0.3.5)
$ node
> require('test')()
Hello, World!
undefined
>

動いたぽい。

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
20