Template Literals from external file

You don't want to read the file, firstly that will read it as a string, and secondly there are better ways of dealing with JS modules.

What you want is to export the temple string as a function from one file and import it into the other.

File 1:

module.exports = (myLink, myID) => `Please visit ${myLink}/${myID} and let them know that ${myID} sent you.`

File 2:

const createString = require('./file1');
console.log(createString('google.com', 'testID'));

I know eval is not much liked but this seems to be the only solution to your question.

eval("`"+str+"`");

Tags:

Javascript