Use fs module in React.js,node.js, webpack, babel,express
I have a requirement in which I am rendering view in which I display a form. On submit of form i need to gather the form data and create a file and save form data as JSON in that file. I am using React.js, node.js, babel and webpack.
After struggling a bit to achieve this I figured out that I have to use isomorphic or universal javascript ie use react and render on server side as we cannot use fs module on client side. Referred this for server side.
i run it using: npm run start
After this I can see in console that [Object Object]
is printed on console from Line 1 in below react component (HomePage.js). But later on when I access this page it gives an error:
'bundle.js:18 Uncaught Error: Cannot find module "fs"'
How can i use fs module with react ?
Below are code snippets :
webpack.config.js
"use strict";
const debug = process.env.NODE_ENV !== "production";
const webpack = require('webpack');
const path = require('path');
module.exports = {
devtool: debug ? 'inline-sourcemap' : null,
entry: path.join(__dirname, 'src', 'app-client.js'),
devServer: {
inline: true,
port: 3333,
contentBase: "src/static/",
historyApiFallback: true
},
output: {
path: path.join(__dirname, 'src', 'static', 'js'),
publicPath: "/js/",
filename: 'bundle.js'
},
module: {
loaders: [{
test: path.join(__dirname, 'src'),
loader: ['babel-loader'],
query: {
//cacheDirectory: 'babel_cache',
presets: debug ? ['react', 'es2015', 'react-hmre'] : ['react', 'es2015']
}
}]
},
plugins: debug ? [] : [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
}),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: { warnings: false },
mangle: true,
sourcemap: false,
beautify: false,
dead_code: true
}),
]
};
Errors
First let's go through your errors a little bit:
When you don't use npm run build
or npm run start
, you won't use webpack and therefore the require
statement doesn't get replaced with the contents of the fs
module--instead you are left with a require statement, which your browser doesn't understand since require is a Node-only function. Thus, your error about require not being defined.
If you do run with npm run build
or npm run start
, webpack takes that require statement out and replaces it with the fs
module. But, as you've discovered, fs
doesn't work on the client side.
Alternatives
So, if you can't use fs
to save files, what can you do?
If you are trying to save the file to a server, you have to submit data from your form to the Node server, and the Node server can use fs
to interact with the server's filesystem to save the file.
If you are trying to save the form locally, ie, on the same device as the browser, you need to use another strategy like this or by using a client-side library like FileSaver. Which option you take depends somewhat on your use case, but if you are trying to save on the client side you can search around "saving files from web browser" or "saving files client side" to see what works for you.
链接地址: http://www.djcxy.com/p/36694.html