Home » Nodejs » Compiling coffeescript on npm install

Compiling coffeescript on npm install

Posted by: admin November 30, 2017 Leave a comment


I’m building an app consisting of private npm repos built in CoffeeScript. To keep deployment language-agnostic, and allow each app to specify its version of CoffeeScript, I’m including CoffeeScript as a dependency in each library, and building into JavaScript upon npm installation.

npm installation works fine for standalone repos, but fails when I try to install a repo that depends on another repo being built.

So if I have repo-a, whose package.json includes this:

"dependencies": {
  "coffee-script": "~1.2.0"
"scripts": {
  "install": "./node_modules/coffee-script/bin/cake install"

and repo-b, whose package.json includes this:

"dependencies": {
  "coffee-script": "~1.2.0",
  "repo-a": "git+ssh://[email protected]:myrepo.git"
"scripts": {
  "install": "./node_modules/coffee-script/bin/cake install"

where both have a Cakefile that looks like this, with an install task called on an npm install hook:

{print} = require "util"
{spawn} = require "child_process"

coffee = "./node_modules/coffee-script/bin/coffee"

echo = (child) ->
  child.stdout.on "data", (data) -> print data.toString()
  child.stderr.on "data", (data) -> print data.toString()

install = (cb) ->
  console.log "Building..."
  echo child = spawn coffee, ["-c", "-o", "lib", "src"]
  child.on "exit", (status) -> cb?() if status is 0

task "install", "Install, build, and test repo", install

npm install works for for repo-a, but fails for repo-b with this message:

sh: ./node_modules/coffee-script/bin/cake: No such file or directory

at which point an unfinished ___coffee-script.npm directory exists in node_modules.

Of course it would be much easier to use a app.js wrapper, but I need to deploy JavaScript, not CoffeeScript. Can anyone tell me how I could get this to work?


Two things.

  1. If you’re running cake from an npm command, you can just specify cake install or cake build as the scripts.install field. This will run after coffee-script has been installed locally, and its bin linked appropriately (with a shim on windows), and will run with a PATH environ such that the locally installed cake is used rather than anything else in the system path.
  2. If you’re not running this from an npm command, but you are nonetheless expecting that coffee-script has already been installed locally via npm (which it looks like), then you should probably be hitting ./node_modules/.bin/cake or ./node_modules/.bin/coffee rather than diving into the package internals.

If you are not installing coffee-script with npm, but instead using some git submodules or something, then you’re on your own 🙂


I would recommend to locally build the JS and store the compiled packages as tar.gz files on S3. For local development you can npm link and in production you point to the archive urls. If you don’t want to version your dependencies you could just have the same url updated over and over.

Keep in mind this npm bug so: https://github.com/isaacs/npm/issues/1727 (you’ll basically have to wipe your node_modules folder everytime you update the deps on an app that uses git/http urls as version numbers).