Import Export Module in nodejs - node.js

In Nodejs, i created a function in th path /js
var myfunction=function(param1){
}
exports.myfunctionA=myfunctionA
In the path routes, I want call this function
myfunctionA=require('./js/myFunctionA')
But I have a message:
Unhandled rejection TypeError: myfunctionA is not a function
Thanks for your help,
Mdouke

You have typo in function name
var myfunctionA=function(param1){}
exports.myfunctionA=myfunctionA
If this file is named functionA.js then You can include a module by
var moduleA = require('./js/functionA')
In this module You have a functionA. You can access to this function by
var functionA = moduleA.functionA
or simpliest way
var functionA = require('./js/moduleA').functionA
If you module only export a one function, then named this file functionA.js and write
exports = function(){}
and access to this function by
functionA = require('./js/functionA')
I hope I help.

you can modify the content of your file in js path as follow
exports.myfunctionA = function(param1){
}
after that in route path, you require:
var myfunction = require('./js/myFunctionA');
and use it:
myfunction.myfunctionA();

Related

Not able to call the function with parameter which was exported from other module in node js

I have helper js file which does a common operation. The first js file is been included in my second js file so that the first file operation can be used across.
In file 1, I have a function which accepts a parameter to it and does some operation. I have exported this function to the module. But when I call this function with parameter I get an error as the exported entity is not a function.
file1.js
var method = function(parameter){
// does some operation
}
module.exports.method=method
file2.js
var fileone = require('path to file1')
var f = new fileone()
f.method(test)
When in try to invoke f.method(test), I am getting an error as the method is not a function.
From the code you provided, the new operator is not required.
Your code should look more like
file1.js
var method = function(parameter){
// does some operation
}
module.exports.method=method
file2.js
var fileone = require('path to file1')
fileone.method(test)

return value to app.js file in node js

I have two files, one called filename and the second called app.js, both files are on the server side. From filename.js filder I am returing a value string from ensureAuthentication method to app.js file, so i export the function:
function ensureAuthentication(){
return 'tesstestest';
}
exports.ensureAuthentication = ensureAuthentication;
in app.js file i do following
var appjs = require('filename');
console.log(appjs.ensureAuthentication);
result is always is undifined in console??! why is that any idea?
You should try this in your app.js -
var login = require('filename');
console.log(login());
or you can use this :
var login = require('filename')();
console.log(login);
Explanation: Whenever you are exporting a function using exports you need to execute it to get the return value from it.
Try this:
var appjs = require('filename');
console.log(appjs.ensureAuthentication());
note the () after the function call. This will execute your function. The console.log() call will then print the returned value.
Try this, make sure both files are in the same directory. You have a few errors with your code. Missing brackets, and not importing correctly in app.js.
filename.js
function ensureAuthentication(){ // You are missing the brackets here.
return 'tesstestest';
}
exports.ensureAuthentication = ensureAuthentication;
app.js
var appjs = require('./filename'); // You are missing the ./ here.
console.log(appjs.ensureAuthentication()); // Logs 'tesstestest'
Two problems with your code:
You need to require with a relative path (notice the ./):
var appjs = require('./filename');
To get the string value you need to invoke ensureAuthentication as a function:
console.log(appjs.ensureAuthentication());
UPDATE
This update addresses the screenshot posted in the comments.
In the screenshot you pasted in the comments, you have the following line:
module.exports = router
That assigns a different exports object to the module. So your local reference to exports is no longer the same object.
Change that line to
module.exports = exports = router
Which will preserve the reference to exports which you use next.
Here you go with the working code
filename.js
function ensureAuthentication(){
return 'tesstestest';
}
module.exports = {
ensureAuthentication : ensureAuthentication
}
app.js
var appjs = require('./utils/sample');
console.log(appjs.ensureAuthentication());

Can't export function expression: "TypeError: xxx is not a function"

I was trying to follow basic guide on modules. I've created test_module.js
var textFunction = function() {
console.log("text");
};
exports = textFunction;
And then I tried to use it in my app.js:
var textFunction = ('./test_module');
textFunction();
But I get an error:
TypeError: textFunction is not a function
Am I doing something wrong? Or it was a very old guide?
PS: exports works only if I declare it like this:
exports.text = function() {
console.log("text");
}
exports is a local variable. Assigning to it won't change the exported value. You want to assign to module.exports directly:
module.exports = textFunction;
module.exports and exports initially refer to the same value (an object), but assigning to exports won't change module.exports, which is what counts. exports exists for convenience.
The other issue is that you are not properly requiring the module, but that may just be a typo. You should be doing
var textFunction = require('./test_module');
var textFunction = ('./test_module'); just assigns the string './test_module' to textFunction and we all know that strings are not functions.

nodejs require statement issue

I'm new to nodejs. I have the following files and code:
// file: myfunc.js
function myfunc() { return "myfunc()"; }
exports = myfunc;
and
// file: index.js
var mf = require("./myfunc");
var mfunc = mf();
console.log(mfunc);
When I run node index.js from command line, I get the error
var mfunc = mf()
^
TypeError: Object is not a function
Why do I get this error? I saw someone else's code which I paste below and I tried to follow the same approach of trying to get require() to return a function instead of an object.
// file: index.js from another app
var express = require('express');
var app = express();
How come require('express') can return a function but require('./myfunc') can't return a function?
It should be...
module.exports = myfunc;
... instead. Quoting the doc:
If you want the root of your module's export to be a function (such as
a constructor) or if you want to export a complete object in one
assignment instead of building it one property at a time, assign it to
module.exports instead of exports.

node.js coffeescript - issues with requiring modules

I'm having another issue with node.js, this time I cannot get my javascript code to recognize that a coffeescript module's class has functions.
In my main file, main.js I have the following code:
require('./node_modules/coffee-script/lib/coffee-script/coffee-script');
var Utils = require('./test');
console.log(typeof Utils);
console.log(Utils);
console.log(typeof Utils.myFunction);
And in my module, test.coffe, I have the following code:
class MyModule
myFunction : () ->
console.log("debugging hello world!")
module.exports = MyModule
Here is the output when I run node main.js :
function
[Function: MyModule]
undefined
My question is, why is my main file loading the correct module, but why is it unable to access the function? What am I doing wrong, whether it be with the coffeescript syntax, or with how I am requiring my module? Let me know if I should clarify my question.
Thanks,
Vineet
myFunction is an instance method, so it won't be accessible directly from the class.
If you want it as a class (or static) method, prefix the name with # to refer to the class:
class MyModule
#myFunction : () ->
# ...
You can also export an Object if the intention is for all methods to be static:
module.exports =
myFunction: () ->
# ...
Otherwise, you'll need to create an instance, either in main:
var utils = new Utils();
console.log(typeof utils.myFunction);
Or, as the export object:
module.exports = new Utils

Resources