How do I read a local Markdown file in Node?

Viewed 10014

I am using a library called react-markdown. If you know React, it's implementation is very simple.

import React from 'react';
import ReactDOM from 'react-dom';
import ReactMarkdown from 'react-markdown';

var content = '# This is a header\n\nAnd this is a paragraph';

ReactDOM.render(
  <ReactMarkdown source={content} />,
  document.getElementById('root')
);

The demo for the library only discusses creating the Markdown string as a variable, not an locally located file. The obvious thing to me to do was to use fs in Node. So I wrote the following:

import React from 'react';
import ReactDOM from 'react-dom';
import ReactMarkdown from 'react-markdown';
import * as fs from 'fs';
import content from './content/generic.md';

fs.readFile(content, (err, data) => {
  if (err) throw err;
  console.log(data);
});

Before I can provide the ReactMarkdown element with content, Webpack complains that readFile is not a function!

Specifically, it highlights the fs.readFile line and says:

TypeError: __WEBPACK_IMPORTED_MODULE_5_fs___default.a.readFile is not a function

This same error occurs if I use import {readFile } from 'fs' and it also occurs when I use fs.stat or fs.open. Isn't fs a default library for node? Should it be included in package.json for the webpack? Even if I run npm install --save fs I still can't access these functions. (P.S. I am using create-react-app)

Moreover, if I can't do this with fs, is there another method?

1 Answers
Related