2
votes

I've been developing Node apps and packages for a while, and have always written in CoffeeScript.

To use npm, I generally do a local install of coffee-script, then set a script within package.json to run the app, as such:

"start": "coffee app.coffee"

Is this the best practice? I understand that there is an alternative of providing coffee-script as a dev dependency, then having a prepublish script to compile coffee -> JS.

1
You mean in terms of publishing to NPM? You should ship JS, not CS (or TS or whatever else). - jonrsharpe
Right; code should always be shipped as JS. So, not in terms of publishing, but more so during development. I'm wondering if it's best to have a start vs prepublish script. With start, coffee is available to run .coffee files directly, whereas prepublish will preprocess coffee -> JS, then have node run as usual with JS files. - matthewhuie

1 Answers

1
votes

The way I do it is to have the coffeescript in a src/ directory and compile it to a lib/ directory.

My .gitignore contains a lib/ line and my .npmignore file contains a single line with src/. Thus the source does not get published to npm and the javascript does not get published to Github.

My package.json file contains the following:

  "main": "lib/main.js",
  "scripts": {
    "watch": "coffee -c -w -o lib src &",
    "compile": "coffee -c -o lib src",
    "prepublishOnly": "npm run-script compile"
  },
  "devDependencies": {
    "coffeescript": "^2.3.2"
  }

So while I'm working, I run npm watch to keep the javascript files up to date. But before publishing, it ensures that the javascript files are up to date.