È possibile creare un file templates.js (manualmente o nel codice) come modulo vuoto. Quindi, dopo aver compilato i modelli, allegare la funzione compilata al modulo vuoto.
var ejs = require('ejs');
var fs = require('fs');
fs.writeFileSync('./template.js', 'module.exports = { }', 'utf8');
var compiled = ejs.compile(fs.readFileSync('./example.ejs', 'utf8'));
// Add an example function to the template file that uses the compiled function
require('./template').example = compiled;
// Get the example template again (this could be in another file for example)
var output = require('./template').example;
var html = output({ id: 10, title: 'Output' });
Dal modules are cached per impostazione predefinita, si dovrebbe essere in grado di require('./template.js')
ovunque ne abbiate bisogno e avrà tutti i modelli precompilati allegati.
Questo non è correlato alla serializzazione di modelli, che era la domanda reale. – Avius