node.js 节点在使用 webpack 时找不到模块“fs”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39249237/
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
Node cannot find module "fs" when using webpack
提问by user3799968
I'm using node.js and webpack to create a bundle. From what I've read, node.js should contain fsmodule for managing files. However when I call require("fs")I get an Cannot find module "fs"error. What should I do?
我正在使用 node.js 和 webpack 创建一个包。据我所知,node.js 应该包含fs用于管理文件的模块。但是,当我打电话时,require("fs")我收到一个Cannot find module "fs"错误。我该怎么办?
回答by Joe Withey
I came across this problem myself when bundling with webpack and found the answer on this thread.
我自己在与 webpack 捆绑时遇到了这个问题,并在这个线程上找到了答案。
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'
};
By setting target to node webpack will make the necessary changes to bundle your node application
通过将 target 设置为 node webpack 将进行必要的更改以捆绑您的 node 应用程序
Edit: This answer targeted webpack 1.x which has now been superseded.
编辑:此答案针对现已被取代的 webpack 1.x。
回答by PDG
I had the same issue when bundling a NWjs application using webworkers(which in turn had node enabled).
使用 webworkers(反过来启用了节点)捆绑NWjs 应用程序时,我遇到了同样的问题。
The solution I found was to include each native module I used in externalswith the prefix commonjsto the name of the module. For example:
我找到的解决方案是将我使用的每个本机模块都包含 在模块名称externals的前缀commonjs中。例如:
...
target: "webworker", // or 'node' or 'node-webkit'
externals:{
fs: "commonjs fs",
path: "commonjs path"
}
...
I've done the same for targets "webworker" and "node-webkit" in different projects to solve the same issue.
我对不同项目中的目标“webworker”和“node-webkit”做了同样的事情来解决相同的问题。
回答by Hemadri Dasari
If you are running your webpack bundle in nodejs environment then target: 'node' is required in webpack.config.js file otherwise webpack takes default value as web for target check here.
如果您在 nodejs 环境中运行 webpack 包,则 target: 'node' 在 webpack.config.js 文件中是必需的,否则 webpack 将默认值作为 web 用于目标检查here。
You can resolve the issue in two ways
您可以通过两种方式解决问题
Add below configuration to your webpack.config.js
将以下配置添加到您的 webpack.config.js
node: {
fs: "empty"
}
OR
或者
Add below configuration to your package.json
将以下配置添加到您的 package.json
"browser": {
"fs": false
}
回答by wizulus
I needed to build a class that would use fetchif executed in a browser, or fsif executed in node. For other reasons, it was impractical to produce separate bundles, so I produced a single browser-targeted bundle.
我需要构建一个fetch在浏览器中fs执行或在 node.js 中执行时使用的类。由于其他原因,生成单独的包是不切实际的,所以我生成了一个针对浏览器的包。
The solution I used was to use eval('require("fs")')if the script was running in node.
我使用的解决方案是eval('require("fs")')在脚本在 node.js 中运行时使用。
const fs = eval('require("fs")')
Browser-safe (fsis nullin the browser):
浏览器安全(fs是null在浏览器中):
const fs = typeof window === 'object'
? null
: eval('require("fs")')
回答by Gomino
After trying everything I found on the internet (target, externals, nodeconfigs), the only solution that actually worked for me was replacing:
在尝试了我在互联网上找到的所有内容(target, externals, nodeconfigs)后,唯一对我有用的解决方案是替换:
const filesystem = require("fs")
or
import fs from "fs"
by the special webpack version
通过特殊的 webpack 版本
const fs = __non_webpack_require__("fs")
This generates a require function that is not parsed by webpack.
这会生成一个不被 webpack 解析的 require 函数。
回答by OpSocket
In addition to the answer of PDG
除了PDG的回答
I'm used to this short copy/paste candy.
我已经习惯了这种简短的复制/粘贴糖果。
Using pathand fs:
使用路径和fs:
var nodeModules = {};
fs.readdirSync(path.resolve(__dirname, 'node_modules'))
.filter(x => ['.bin'].indexOf(x) === -1)
.forEach(mod => { nodeModules[mod] = `commonjs ${mod}`; });
// Before your webpack configuration
module.exports = {
...
}
Then inside your configuration file, include the nodeModulesvariable in the externals
那么你的配置文件中,包括nodeModules在变量的外部
...
externals: nodeModules,
...
回答by Brian Smith
For the solution we are building we had to force an older version of webpack:
对于我们正在构建的解决方案,我们必须强制使用旧版本的 webpack:
npm install --save --force webpack@webpack-3

