ubuntu@ip-xxx-xx-x-xxx:/var/www/html$ npm install fs
npm WARN package.json crypto@0.0.3 crypto is also the name of a node core module.
npm WARN package.json filesystem@1.0.1 No description
npm WARN package.json http@0.0.0 http is also the name of a node core module.
npm WARN package.json http@0.0.0 No description
npm WARN package.json http@0.0.0 No repository field.
npm WARN package.json js@0.1.0 No repository field.
npm WARN package.json querystring@0.2.0 querystring is also the name of a node core module.
npm WARN package.json url@0.10.1 url is also the …Run Code Online (Sandbox Code Playgroud) 我有一个要求,我在其中呈现我在其中显示表单的视图.在提交表单时,我需要收集表单数据并创建一个文件,并将表单数据保存为该文件中的JSON.我正在使用React.js,node.js,babel和webpack.
经过努力实现这一点后,我发现我必须使用同构或通用的javascript,即在服务器端使用react和render,因为我们不能在客户端使用fs模块. 将此提交给服务器端.
我运行它: npm run start
在此之后,我可以在控制台中看到[Object Object]从下面的第1行反应组件(HomePage.js)打印到控制台上.但是稍后当我访问此页面时,它会出错:
'bundle.js:18未捕获错误:找不到模块"fs"'
我怎样才能使用fs模块做出反应?
以下是代码段:
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: …Run Code Online (Sandbox Code Playgroud)我正在制作一个项目,需要允许用户从浏览器与文件系统进行交互.我在编写客户端JavaScript方面有很多经验,而且我有很多为Web抓取,数据分析和文件系统工作编写Node脚本的经验.但是这个项目将允许用户在浏览器中更改内容,然后在本地保存这些数据(最终保存到Web服务器) - 我对此没有任何经验.
我已经安装了browserify和browserify-fs以在浏览器中使用Node的fs模块,并使用browserify-fs README中的示例创建目录,向其写入文件,然后读取该文件:
var fs = require('browserify-fs');
fs.mkdir("/home", function(err){
if (err) throw err;
fs.writeFile("/home/hello-world.txt", "Hello world!", function(err) {
if (err) throw err;
fs.readFile("/home/hello-world.txt", "utf-8", function(err, data) {
if (err) throw err;
console.log(data);
});
});
});
Run Code Online (Sandbox Code Playgroud)
从"Hello world!"记录的意义上说,这"有效".在控制台中.但据我所知,它不会在本地创建目录或保存文件.我有一些模糊的感觉,它是在浏览器中临时保存这些东西,并且当我离开时它们被删除.但我想实际创建一个目录并在本地保存一个文件.我可以单独使用JavaScript吗?是否有关于如何在基于浏览器的JavaScript和Node之间"关闭循环"的好教程?
我接受了TJ Crowder的回应 - ExpressJS确实在JavaScript中进行客户端 - 服务器通信相对简单.我现在正在做的是,我将用户的条目保存到全局JSON对象.当用户单击"保存"按钮时,我使用字符串化的JSON 更新<input>元素中隐藏元素的值<form>.然后我提交表单,Express app.post()和模块体解析器给我一切req.body.然后我可以执行正常的节点文件系统操作.
我正在使用Babel和Webpack开发一个React应用,我想使用file-existsnpm中的软件包。我已经安装了该软件包并将其保存为项目的依赖项。运行npm start后,出现此错误:
./~/file-exists/index.js中的错误找不到
模块:错误:无法在C:\ GitHub \ CryptoPrices \ node_modules \ file-exists
@ ./~/file-exists/index.js中解析模块'fs' 3:9-22
file-exists使用fs的依赖,但由于某种原因,它不工作。如果我不需要Npm,Npm可以正常启动和运行file-exists。
这是我的webpack配置文件:
module.exports = {
entry: [
'./src/index.js'
],
output: {
path: __dirname,
publicPath: '/',
filename: 'bundle.js'
},
module: {
loaders: [{
// exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['react', 'es2015', 'stage-1']
}
}]
},
resolve: {
extensions: ['', '.js', '.jsx']
},
devServer: {
historyApiFallback: true,
contentBase: './'
}
};
Run Code Online (Sandbox Code Playgroud)
我是Webpack和Babel的新手,所以我有点迷路。