Reputation: 55
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?
Upvotes: 1
Views: 1055
Reputation: 164
Your importing and exporting seems to be correct.
Just change this
console.log(syn(code));
To this
console.log(mysyn(code));
Upvotes: 2
Reputation: 5845
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');
Upvotes: 1