0

I'm working to create a shared package of JavaScript functions. At this time, I'm trying to use them like this:

/app/index.js

const myPackage = require('../myPackage');

myPackage.function1();
myPackage.myScope.function2();

The above successfully loads myPackage. However, when I attempt to run function1, I receive an error that says: "TypeError: myPackage.function1 is not a function". My code in the "package" is organized like this:

/myPackage
 index.js
 root
   function1.js
 myScope
   function2.js

The code looks like this:

index.js

require('./root/function1.js');
require('./myScope/function2.js');

function1.js

exports.function1 = function() {
  console.log("Doing stuff in function1");
}

function2.js

exports.function2 = function() {
  console.log("Doing stuff for function2");
}

I could understand function2 not working because, there's nothing putting it in myScope, which I don't know how to do. However, I don't understand why function1 isn't running. What am I doing wrong?

0

2 Answers 2

1
+50

To elaborate bergi's answer, you need to have the following in your index.js file:

// file: index.js
exports.function1 = require('./root/function1.js').function1;
exports.myScope2 = {
    function2: require('./myScope/function2.js').function2,
};

Because require('./root/function1.js') == exports object in function1.js. So if you have multiple functions in your function1.js, you have to go like this:

// file: index.js
exports.function1 = require('./root/function1.js').function1;
exports.function11 = require('./root/function1.js').function11;
exports.function111 = require('./root/function1.js').function111;
...

A shortcut of that can be:

// file: index.js
Object.assign(exports, require('./root/function1.js'));

On the other hand: you can set the exports object to be your function:

// file: function1.js
module.exports = function() {
    console.log("Doing stuff in function1");
}

Then you can have the following in your index.js file:

// file: index.js
exports.function1 = require('./root/function1.js');
exports.myScope2 = {
    function2: require('./myScope/function2.js'),
};

Here require('./root/function1.js') == function1 from function1.js. Hope that explains the issue.

Sign up to request clarification or add additional context in comments.

6 Comments

@MunimMunna What does the /app/index.js file look like then? When I try this, I still get runtime errors.
index.js looks exactly like the first code-block in the answer, just copy paste. What error do you get?
@MunimMunna I see "TypeError: myPackage.function1 is not a function". I added a console.log(myPackage) just before the call to see what was there. I see this: { function1: undefined }
@MunimMunna I should mention that if I use require('./root/function1.js') instead of require('./root/function1.js').function1 I see { function1: {} } from the console.log. However, I see the same error.
@MunimMunna I got it working. I had to add module.exports = function1; to the bottom of function1.js.
|
1

Your index.js doesn't export anything. You will have to do

Object.assign(exports, require('./root/function1.js'));
exports.myScope = require('./myScope/function2.js');

Or maybe better have your function1.js and function2.js modules export the function itself (module.exports = function() { … };) instead of creating a property, then use

exports.function1 = require('./root/function1.js');
exports.myScope = {
    function2: require('./myScope/function2.js'),
};

8 Comments

If I use exports.function1 = ..., I then have to use myPackage.function1.function1 in my code. I just want to use myPackage.function1.
@user687554 You'd do that if you change the export inside the /root/function1.js - a file with that name would be expected to export only that function, not a module object containing that function.
So, "function1.js" would be changed to just function function1() { console.log("Doing stuff in function1"); } Is that correct?
No, to modules.export = function function1() { …};. The important part is omitting the property from the assignment.
I'm sorry, I don't understand what you mean. Which assignment? What should index.js look like?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.