I have some code written in javascript that I want to execute within my program that is written in Coffeescript. Is there a way I can use the Javascript as-is inside my coffeescript file or do I need to re-write the function so it works in Coffesscript?

1

Best Answer


If you want to execute your code directly within a compiled CoffeeScript file you'll almost certainly need to rewrite your Javascript as (aside from arrays) the syntax in CoffeeScript is completely different: Primitives, objects, logical operators and so on.

You can however import external Javascript files using require and run functions, as outlined in this answer. Here's quick example with the linked answer as reference:

Your CoffeeScript file:

myExternalJs = require('myExternalJs')class Examplecreate: -> myExternalJs.myExternalFunction()supermodule.exports = Example

And then your Javascript file named myExternalJs.js:

function myExternalFunction () {console.log('hello from a normal javascript function!')};exports.myExternalFunction = myExternalFunction;