require file as string

Stringnode.jsExpressRequire

String Problem Overview


I'm using node + express and I am just wondering how I can import any file as a string. Lets say I have a txt file all I want is to load it into a variable as such.

var string = require("words.txt");

I am against

modules.exports = function(){

    var string = "whatever";
    
    return string;

}

String Solutions


Solution 1 - String

If it's for a (few) specific extension(s), you can add your own require.extensions handler:

var fs = require('fs');

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

var words = require("./words.txt");

console.log(typeof words); // string

Otherwise, you can mix fs.readFile with require.resolve:

var fs = require('fs');

function readModuleFile(path, callback) {
    try {
        var filename = require.resolve(path);
        fs.readFile(filename, 'utf8', callback);
    } catch (e) {
        callback(e);
    }
}

readModuleFile('./words.txt', function (err, words) {
    console.log(words);
});

Solution 2 - String

To read the CSS file to String, use this code. It works for .txt.

const fs = require('fs')
const path = require('path')

const css = fs.readFileSync(path.resolve(__dirname, 'email.css'), 'utf8')

ES6:

import fs from 'fs'
import path from 'path'

let css = fs.readFileSync(path.resolve(__dirname, 'email.css'), 'utf8')

Solution 3 - String

The selected answer is deprecated and not recommended anymore. NodeJS documentation suggests other approaches like:

> loading modules via some other Node.js program

but it does not expand any more.

  • You can use a very simple library like this: require-text

  • Or implement it yourself ( like from the package above: ) > js > var fs = require('fs'); > module.exports = function(name, require) { > return fs.readFileSync(require.resolve(name)).toString(); > }; >

Solution 4 - String

you'll have to use readFile function from filesystem module.

http://nodejs.org/docs/v0.3.1/api/fs.html#fs.readFile

Solution 5 - String

My simplest solution is

var string = require("fs").readFileSync("file.txt", 'utf8')
console.log("string = ", string);

Solution 6 - String

you can require .json files, both with node.js and TypeScript. That's the only format that support being required() suitable for serializing text. YOu can use a compile-time tool to pack your files into a json, such as https://github.com/cancerberoSgx/fs-to-json

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThomasReggiView Question on Stackoverflow
Solution 1 - StringJonathan LonowskiView Answer on Stackoverflow
Solution 2 - StringMax MaView Answer on Stackoverflow
Solution 3 - StringmimView Answer on Stackoverflow
Solution 4 - StringSimon BoudriasView Answer on Stackoverflow
Solution 5 - StringMinh NguyenView Answer on Stackoverflow
Solution 6 - StringcancerberoView Answer on Stackoverflow