Can I write an npm package in coffeescript?

I used coffeescript for a while. Now I need to write an npm package, can I write it to coffeescript, or should I compile coffeescript to javascript?

+67
npm coffeescript
Nov 30 '12 at 13:01
source share
5 answers

I am going to suggest that you write your package in coffeescript, but publish it only in javascript. I do it like this:

  • coffeescript code is in src
  • code compiled into lib
  • src tied to my git repo, lib is in my .gitignore
  • lib published before npm, src is in my .npmignore
  • coffee-script package is in my devDependencies

You can take a look at a simple package, refix , for inspiration:

+98
Nov 30 '12 at 13:35
source share

You can write NPM modules in coffeescript, but in order for them to be used by JS users, they must be compiled into JS before you publish to NPM.

package.json does this with a prepublish script hook that runs the specified script before publishing. Here is an example prepublish NPM hook in zombie.js

https://github.com/assaf/zombie/blob/master/package.json#L16

+10
Nov 30
source share

I wrote npm packages in CoffeeScript from scratch. I recommend using CoffeScript for node as well as for browser. However, before you can use or publish your module, you need to compile CoffeeScript source code for JavaScript. However, this should not delay you from using CoffeeScript.

Tip. During development, use coffee -cw yourfile.coffee (command line) to view the file for changes and compile when saved.

+1
Nov 30
source share

While I'm not sure if this is the best approach, you can technically write your package mainly in CoffeeScript.

Basically, you can write a JS file that simply wraps the coffee command, for example:

bin /howl.coffee

 console.log 'Awwwooooo!' 

bin / howl.js

 #!/usr/bin/env node var path = require('path'); var exec = require('child_process').exec; var coffee = path.resolve(__dirname, '../node_modules/coffee-script/bin/coffee'); var howl = path.resolve(__dirname, './howl.coffee'); var command = coffee + ' ' + howl; exec(command, function(error, stdout) { if (error) { throw error }; console.log(stdout); }); 

Running node howl.js (or just howl when installed globally) now displays Awwooooo! . You can do something like require other CoffeeScript files and access the arguments by passing them from the JavaScript wrapper to CoffeeScript.

In any case, there may be reasons not to do this, but it has worked for me so far, so I decided to present this for an additional perspective.

For a simple example using this method, check out https://www.github.com/joshuabc/packdown .

0
Jan 07 '15 at 6:39
source share

If your modules have coffee-script in devDependencies , it is useful to simply install coffee-script globally instead of installing it for each module (which takes a lot longer).

coffee-build is a global version manager for coffee-script .

Just add these 2 scripts to your package.json :

 { "name": "my-coffee-module", "scripts": { "build": "coffee-build -v 1.11.x -b -o js src", "postinstall": "npm run build" } } 

Note that -v 1.11.x not an exact version that allows implicit updates.

The only downside is that users need npm install -g coffee-build before they can install your module.

0
Oct 11 '16 at 17:25
source share



All Articles