Sunday 21 May 2017

In node/React how can I read in a local markdown file to a string

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 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

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



via russellmania

No comments:

Post a Comment