找不到模块:错误:无法解析模块“fs" [英] Module not found: Error: Cannot resolve module 'fs'
问题描述
我正在使用 Babel 和 Webpack 制作一个 React 应用程序,我想使用 npm 中的 file-exists
包.我已经安装并保存了该包作为我项目的依赖项.运行 npm start 后,我收到此错误:
I'm making a react app using Babel and Webpack and I want to use the file-exists
package from npm. I already installed and saved the package as a dependency for my project. After running npm start I get this error:
./~/file-exists/index.js 中的错误
找不到模块:错误:无法解析 C:GitHubCryptoPrices
ode_modulesfile-exists
中的模块fs"@ ./~/file-exists/index.js 3:9-22
ERROR in ./~/file-exists/index.js
Module not found: Error: Cannot resolve module 'fs' in C:GitHubCryptoPrices ode_modulesfile-exists
@ ./~/file-exists/index.js 3:9-22
file-exists
使用 fs
作为依赖项,但由于某种原因它不起作用.如果我不需要 file-exists
任何地方,Npm 启动和运行没有任何问题.
file-exists
uses fs
as a dependency but for some reason it is not working. Npm starts and runs without any issues if I don't require file-exists
anywhere.
这是我的 webpack 配置文件:
here is my webpack config file:
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: './'
}
};
我是 Webpack 和 Babel 的新手,所以我有点迷茫.
I'm new to Webpack and Babel so I'm a little bit lost.
推荐答案
看起来你在你的 index 中调用了
文件.我不确定您在什么上下文中调用该方法,但这看起来像是对服务器端方法的客户端调用.我最近遇到了类似的问题.fs
file-exists
方法.js
It looks like you're calling the fs
file-exists
method in your index.js
file. I'm not sure in what context you're calling the method, but this looks like a client-side call to a server-side method. I ran into a similar issue just recently.
据我了解,主要问题似乎是您无法在浏览器解释的客户端(前端)代码中调用服务器端(节点)方法.您必须从服务器调用它们.
From what I understand, the main issue seems to be that you can't call server-side (Node) methods in browser-interpreted client-side (front end) code. You have to call them from a server.
Webpack 可以将 fs
模块代码加载到您的前端,但浏览器实际上无法解释这些 Node 方法并运行它们;只有 Node 环境可以做到这一点.(更多在这里)
Webpack can load the fs
module code into your front end, but the browser can't actually interpret those Node methods and run them; only a Node environment can do that. (more here)
您可以通过修改对 fs
方法的调用以在服务器端某处发生,或者通过查找提供与 fs 相同功能的等效浏览器支持包来解决核心问题
您需要的方法,但可以在浏览器中运行.
You could fix the core issue by modifying the call to fs
methods to happen somewhere server-side, or by finding an equivalent browser-supported package that offers the same functionality as the fs
methods you need but that can be run in a browser.
快速搜索浏览器的 fs 模块"会显示各种可能满足您需求的选项,例如 fs-web、browserify-fs 或
A quick search for "fs module for browser" brings up a variety of options that might work for what you need, like fs-web, browserify-fs or filer.
这是一个类似的问题,但有一些见解.在 React.js,node.js 中使用 fs 模块.js、webpack、babel、express
Here is a similar question with some insight. Use fs module in React.js,node.js, webpack, babel,express
这篇关于找不到模块:错误:无法解析模块“fs"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!