I have a javascript function in "sample.js" file. It is like this:
var mapDict = { '100': 'test_100.js', '200': 'test_200_API.js', '300': 'test_300_API.js' }
function mapAPI()
{
this.version = 0.1;
}
mapAPI.prototype.getFileName = function( id ) {
return mapDict[id]
}
module.exports = mapAPI;
in another file named "institute.js" I want to require the above "test_xxx_API" files dynamically. I have the following code:
const mapAPI = require('../../sample.js');
const map = new mapAPI();
const mapFile = map.getFileName("100");
var insAPI = require(mapFile);
When I run this code by "node institute.js" command, I get the following error:
Error: Cannot find module './test_100_API.js'.
But the "test_100_API.js" file exists and is located in the current folder besides "institute.js". When I changed var insAPI = require(mapFile);
to var insAPI = require("./test_100_API.js");
and give it the exact path instead of dynamic path, it works fine. Can anyone help me?
Thanks in advance