Requiring CoffeeScript file from within another Co

2019-07-10 17:40发布

问题:

I was wondering if you are able to require a coffeescript from within another coffeescript.

My example code from file "user.coffee"

class UserObj
    constructor: (@name) ->
        console.log @name

My example code from main file

require "./user.coffee"

User = new UserObj "Example"

Is this possible from within a coffeescript file or just a js file?

回答1:

Yes it is possible.

user.coffee:

exports.UserObj = 
class UserObj
    constructor: (@name) ->
        console.log @name

main.coffee:

{UserObj} = require "./user"

User = new UserObj "Example"


回答2:

Please follow this link for details: http://coffeescript.org/documentation/docs/command.html



回答3:

You can use this library coffee-stir to include other dependencies. Its a way around only using required from the web and it will add dependencies as you use it. FYI i made the lib when i was facing similar problems