Michaël Doukan
Michaël Doukan

Reputation: 45

Import Export Module in nodejs

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

Upvotes: 0

Views: 158

Answers (2)

Toandd
Toandd

Reputation: 320

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();

Upvotes: 0

Tomasz Jakub Rup
Tomasz Jakub Rup

Reputation: 10680

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.

Upvotes: 1

Related Questions