Can I write npm package in CoffeeScript?

node.jsCoffeescriptNpm

node.js Problem Overview


I have used CoffeeScript for a while. Now I need to write a npm package, can I write it in CoffeeScript, or I should compile CoffeeScript into JavaScript?

node.js Solutions


Solution 1 - node.js

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

  • coffeescript code goes in src
  • code is compiled to lib
  • src is committed to my git repo, lib is in my .gitignore
  • lib is published to npm, src is in my .npmignore
  • the coffee-script package is in my devDependencies

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

Solution 2 - node.js

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

package.json makes this easy with their prepublish script hook which runs the specified script before you publish. Heres an example of a prepublish NPM hook in zombie.js

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

Solution 3 - node.js

If a lot of your modules have coffee-script in their devDependencies, it's useful to just globally install coffee-script instead of install it for each module (which takes much 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"
  }
}

Notice how -v 1.11.x is not an exact version, which allows implicit upgrades.

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

Solution 4 - node.js

I have written npm packages in CoffeeScript from scratch. I encourage you to use CoffeScript for node as well as for the Browser. However, before you can use or publish your module, you have to compile the source CoffeeScript to JavaScript. That should not hold you back from using CoffeeScript, though.

Tip: While developing, use coffee -cw yourfile.coffee (command line) to watch the file for changes and compile on save.

Solution 5 - node.js

While I'm not sure if it's the best approach, technically it is possible to write your package mostly in CoffeeScript.

Basically, you can write a JS file that simply wraps the coffee command, like so:

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 simply howl when it's installed globally) will now output Awwooooo!. You can do things like require other CoffeeScript files and access arguments by passing them from the JavaScript "wrapper" to the CoffeeScript.

Anyway, there may be reasons not to do this, but it has worked for me so far so figured I'd submit this for an additional perspective.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZhe ChenView Question on Stackoverflow
Solution 1 - node.jsLinus ThielView Answer on Stackoverflow
Solution 2 - node.jsJoseph MonizView Answer on Stackoverflow
Solution 3 - node.jsaleclarsonView Answer on Stackoverflow
Solution 4 - node.jsdavidpfahlerView Answer on Stackoverflow
Solution 5 - node.jsjrhView Answer on Stackoverflow