找不到模块:错误:无法解析模块"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:\ GitHub \ CryptoPrices \ node_modules \ file-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:\GitHub\CryptoPrices\node_modules\file-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.js
文件中调用fs
file-exists
方法.我不确定在什么上下文中调用该方法,但这看起来像是对服务器端方法的客户端调用.我最近遇到了类似的问题.
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节点中使用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屋!