jab
jab

Reputation: 5823

Javascript equivalent to python __init__.py

In python, in order to expose top-level functionality in a package, one would create an __init__.py

#__init__.py
from .implmentation import impl_function

def exposed_fn():

  """call impl_function

Which would expose exposed_fn as the main function to use when importing a directory (package). What's the equivlalent of this in javascript's require?

Obviously you can do the following.

//init.js?
var impl_function = require('./implmentation.js').impl_function;

var exposed_fn = function () {//call impl_function ...};

//Will expose `exposed_fn` when requiring this file.
module.exports = {
  exposed_fn: exposed_fn
}

//How to expose `expose_fn` when requiring a this folder?????

Is there an equivalent? All searches so far haven't proved fruitful.

Upvotes: 8

Views: 5774

Answers (1)

jab
jab

Reputation: 5823

Using an index.js file in the same manner seems to work. If you export objects from an index.js file they can be accessible at the folder level.

Upvotes: 13

Related Questions