Categories
JavaScript Answers

How to require file as string with Node.js?

Spread the love

To require file as string with Node.js, we call readFileSync.

For instance, we write

import fs from "fs";
import path from "path";

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

to read the email.css file into a string with the readFileSync method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *