Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 527
jab Avatar asked Feb 05 '23 12:02

jab


1 Answers

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.

like image 172
jab Avatar answered Feb 07 '23 13:02

jab