In Node.js, how to import functions from another JavaScript file?

In Node.js, how to import functions from another JavaScript file like source common.sh in Bash?

asked Aug 3, 2016 by anonymous

1 Answer

In the .js file to be imported such as 'common.js', organize functions to be exported like

module.exports = {
  func1: function () {
    // func1 impl
  },
  func2: function () {
    // func2 impl
  }
};

In the other .js files, you can "include" the 'common.js' by

var common = require('./common');

In the following code, the functions can be used like

common.func1();
answered Oct 8, 2016 by Eric Z Ma (44,280 points)
edited Oct 8, 2016 by Eric Z Ma

Please log in or register to answer this question.

Related questions

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...