Reading a groovy file in NodeJS


We can read any text file in NodeJS. This post shows you how to read .groovy file's content as text in NodeJS system and use it in a .js file.

Register the /groovy extension

Register groovy extension in your NodeJS app using following code

require.extensions['.groovy'] = function (module, filename) {
    module.exports = fs.readFileSync(filename, 'utf8');
};

Write your .groovy file

Copy paste, or write your xxx.groovy file.

Import .groovy file

In your JS file you can read the .groovy file's content with following code

const groovyFileContent = require('./xxx.groovy');
console.log('groovyFileContent:' + groovyFileContent);