Is there a recommended way of developing a node module if I want to write it in Coffeescript, but don't want to force the module's users to require the coffee-script
module?
Nodejs Third Party Modules: Examples of third party modules are express, mongoose, etc. To install third party modules refer to the previous blog where we have discussed how to install modules using npm.
Put your CoffeeScript codes in the src
folder and the compiled JavaScript codes in lib
folder.
Then in your package.json
file, declare main
to be the js file in the lib
folder. Then the users of your package will require the js file instead of the coffee file.
You may take @TrevorBurnham's repository as an example.
I ended with only a src
folder on my git repository; a .gitignore
file with an line for lib
; and an empty .npmignore
file. The empty .npmignore
file is needed because if it's not on your module, your .gitignore
is used instead.
I just added a Cakefile
with a task to build my src
directory using coffee --compile --output lib/ src/
and a pretest
and prepublish
task to package.json
to build before testing and publishing.
"scripts": {
"pretest": "cake build",
"prepublish": "cake build",
}
This solution keeps my git repository clean (without compiled code), but adds my javascript code to lib when publishing to npm.
I'm just getting started with CoffeeScript, but I'd suggest the following:
src/*.coffee
,main.js
in the project's root that NPM will catch, and have it simply do something like require('coffee-script'); require('./src/my_lib.coffee')
.There. You never, ever compile your code; it's all handled transparently. You don't check compiled code into git, nor do you publish superfluous compiled JavaScript alongside the uncompiled CoffeeScript to NPM.
Edit:
In more recent versions coffee-scirpt, you should require('coffee-script/register');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With