问题描述
我正在使用 node.js 和 webpack 创建一个包.据我所知,node.js 应该包含用于管理文件的 fs
模块.但是,当我调用 require("fs")
时,出现 Cannot find module "fs"
错误.我该怎么办?
I'm using node.js and webpack to create a bundle. From what I've read, node.js should contain fs
module for managing files. However when I call require("fs")
I get an Cannot find module "fs"
error. What should I do?
推荐答案
我自己在与 webpack 捆绑时遇到了这个问题,并在 这个话题.
I came across this problem myself when bundling with webpack and found the answer on this thread.
为我解决它的方法是使用以下配置:
The way to solve it for me was to use the following config:
module.exports = {
entry: "./app",
output: {
path: __dirname,
filename: "bundle.js"
},
module: {
loaders: [
{
test: /.js$/,
exclude: 'node_modules',
loader: 'babel',
query: {presets: ['es2015']},
}
]
},
target: 'node'
};
通过将目标设置为 node webpack 将进行必要的更改以捆绑您的 node 应用程序
By setting target to node webpack will make the necessary changes to bundle your node application
此答案针对现已被取代的 webpack 1.x.
This answer targeted webpack 1.x which has now been superseded.
这篇关于节点找不到模块“fs"使用 webpack 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!