1

This below is my index.js, the file where i'm trying to call an imported function

const mysyn = require('./syntax.js')
router.post("/",(req,res)=>
{
var code = (req.body.Code);
console.log(syn(code));
res.send("POST") 
}
);

and this is syntax.js:

const syn = function(code)
{
   console.log("In sep html"+ code );
}
module.exports = syn;

I tried using ES6 import statement but that didn't work since node throws back the 'Unexpected token {' error. So, How do I come across this?

2 Answers 2

2

Your importing and exporting seems to be correct.

Just change this

console.log(syn(code));

To this

console.log(mysyn(code));
Sign up to request clarification or add additional context in comments.

Comments

1

In order to import a function, you have to export the function.

in syntax.js, you need to export the function like this:

module.exports = syn

And in your index.js file you have to assign the exported function into a local variable like this:

const mySynFn = require('./syntax.js');

2 Comments

i tried that too. But, i have a doubt, do we have to somewhere use mySynFn while calling the 'syn' function?
In your first block, just do const syn = require('./syntax.js'); You're exporting the whole function so when you require it, that IS the function. If you want to export more things from that file later, you can export it under a key: module.exports = { syn } Then when you require it: const mySyn = require('./syntax.js'); mySyn.syn();

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.