0

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

2 Answers 2

1

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.

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

2 Comments

Thanks for your response. It!s not the problem. If a fonction is declared in a folder, normailly it's possible to import in a other folder whith the declaration require('../js/myfunctionA') ?
I improved the answer.
0

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

Comments

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.