NodeJS 我如何在webpack 4.46.0中使用fs?

yhxst69z  于 2022-12-29  发布在  Node.js
关注(0)|答案(1)|浏览(152)

我正在尝试使用fs模块,但收到此错误

Module not found: Error: Can't resolve 'fs' in '/home/pau/Escritorio/Master/Blockchain/Prac2/PRAC2_Template/Ejercicio_2/app/src'
 @ ./src/index.js 5:11-24

我已经安装了文件系统,就我所看到的,有一个问题与webpack,使这种情况发生。
我的package.json是这样的

{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "scripts": {
    "build": "webpack",
    "dev": "webpack-dev-server"
  },
  "devDependencies": {
    "copy-webpack-plugin": "^5.0.5",
    "webpack": "^4.41.2",
    "webpack-cli": "^3.3.10",
    "webpack-dev-server": "^3.9.0"
  },
  "dependencies": {
    "fs": "0.0.1-security",
    "ipfs-http-client": "^47.0.0",
    "web3": "^1.8.1"
  }
}

我的webpack.config.js是这样的

const path = require("path");
const CopyWebpackPlugin = require("copy-webpack-plugin");

module.exports = {
  entry: "./src/index.js",
  output: {
    filename: "index.js",
    path: path.resolve(__dirname, "dist"),
  },
  plugins: [
    new CopyWebpackPlugin([{ from: "./src/index.html", to: "index.html" }]),
  ],
  devServer: { contentBase: path.join(__dirname, "dist"), compress: true },

  
};

我发现了这个链接https://github.com/webpack/webpack/issues/13498,他们说添加

module: {
    rules: [
      {
        test: /@aws-sdk\/lib-storage\//,
        resolve: {
          alias: {
            './runtimeConfig': './runtimeConfig.browser',
          },
        },
      },
    ],
  },

问题消失了,但它对我不起作用。我的webpack.config.js与该代码看起来像这样:

const path = require("path");
const CopyWebpackPlugin = require("copy-webpack-plugin");

module.exports = {
  entry: "./src/index.js",
  output: {
    filename: "index.js",
    path: path.resolve(__dirname, "dist"),
  },
  plugins: [
    new CopyWebpackPlugin([{ from: "./src/index.html", to: "index.html" }]),
  ],
  devServer: { contentBase: path.join(__dirname, "dist"), compress: true },

  module: {
    rules: [
      {
        test: /@aws-sdk\/lib-storage\//,
        resolve: {
          alias: {
            './runtimeConfig': './runtimeConfig.browser',
          },
        },
      },
    ],
  },
};

但我得到了同样的错误。
我该如何解决这个问题?

lhcgjxsq

lhcgjxsq1#

fs是Node.js中的一个内置模块,您只需使用它,如下所示:

const fs = require('fs');

然后你就可以使用它。
事实上,您的package.json中有fs可能会干扰内置模块。您应该从package.json中删除fs,然后运行npm install删除任何可能不必要安装的软件包。
在那之后它应该工作。

相关问题