node.js Webpack nodejs fs.readFile 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37319299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Webpack nodejs fs.readFile is not a function
提问by gamer
I have a webpack config like:
我有一个 webpack 配置,如:
var path = require('path')
module.exports = {
entry: "./index.js",
output: {
path: path.join(__dirname, 'static'),
filename:'bundle.js'
},
module: {
loaders: [
{ test: /\.js$/, exclude: /node_modules/, loader: "babel-loader"},
{ test: /\.json$/, loader: 'json-loader' },
]
},
node: {
fs: "empty"
}
};
And I want to read a file using fs
我想使用读取文件 fs
I am doing something like:
我正在做类似的事情:
var fs = require('fs')
console.log(fs)
fs.readFile('input.txt', function (err, buffer) {
console.log("buffer")
console.log(buffer)
})
I just want to read a file here but when I do this it gives me error saying:
我只想在这里读取一个文件,但是当我这样做时,它给了我错误提示:
fs.readFileis not a function
fs.readFile不是函数
I have installed fsusing npm install --save fs
我已经安装fs使用npm install --save fs
Also when I print fsit gives me empty object.
Above I have done console.log(fs)it is giving me empty object
此外,当我打印fs它时,它给了我空的对象。上面我已经完成了console.log(fs)它给了我空的对象
What is wrong in here?
这里有什么问题?
回答by Viraj Shelke
Your code looks fine! So there is nothing wrong with the code. To cross verify try running the same code here https://www.tutorialspoint.com/execute_nodejs_online.phpand you can see that console.log(fs);won't be blank.
你的代码看起来不错!所以代码没有任何问题。要交叉验证,请尝试在此处运行相同的代码https://www.tutorialspoint.com/execute_nodejs_online.php,您可以看到它console.log(fs);不会是空白的。
So the reason that your code is showing error is because you installed the node module named 'fs' using the command npm install --save fswhich you shouldn't! Node comes up with this module by default. All you got to do is just require is using const fs = require('fs');(using var, or letis also acceptable but I will recommend consthere).
因此,您的代码显示错误的原因是您使用npm install --save fs不应该使用的命令安装了名为“fs”的节点模块!Node 默认自带这个模块。您所要做的就是使用const fs = require('fs');(使用var, 或者let也可以接受,但我会const在这里推荐)。
So just delete the local node_modulesfolder and then run this code again by installing other modules that you want and it will run fine!
因此,只需删除本地node_modules文件夹,然后通过安装您想要的其他模块再次运行此代码,它会运行良好!
Hope this helps :)
希望这可以帮助 :)
回答by Brenn
As I believe the comments mentioned,
正如我相信提到的评论,
node: { fs: "empty" }
Needs to be removed. Moreover, all your code must run on the server. You cannot read files like this in the browser. Node APIs are server-side only, so you would need to build an API using express or some similar library e.g.
需要移除。此外,您的所有代码都必须在服务器上运行。您无法在浏览器中读取此类文件。节点 API 仅适用于服务器端,因此您需要使用 express 或一些类似的库来构建 API,例如
router.get('/file/read', (req, res, next) => { fs.readFile('input.txt', function (err, buffer) {
console.log("buffer")
res.send(buffer);
})
})
Then in your browser you would need to use AJAX to call the API and retrieve the file contents.
然后在您的浏览器中,您需要使用 AJAX 来调用 API 并检索文件内容。
Reading arbitrary files has OBVIOUS SECURITY ISSUES. You should not allow the api to read any files the user likes. You should be very careful here to restict input and limit the set of files available.
读取任意文件有明显的安全问题。您不应该允许 api 读取用户喜欢的任何文件。您应该非常小心地限制输入并限制可用的文件集。

