0

I, am trying to use the fs module but I get this error

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

I have fs installed and for what I've seen, theres a problem with webpack that makes this happen.

Mi packeage.json is this

{
  "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"
  }
}

and my webpack.config.js this

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 },

  
};

I found this link https://github.com/webpack/webpack/issues/13498, where they say that adding

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

The problem disappear, but it doesnt work for me. My webpack.config.js with that code looks like this:

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',
          },
        },
      },
    ],
  },
};

But I get the same error.

How can I solve this?

P00
  • 11
  • 4
  • What does your index.js code look like? Especially line 5, since that is where the error occurs. Also, you don't need to include `fs` in the package.json, because it is a built-in module in the current and LTS Node.js versions. `fs` only remains on npm, so that the name is not misused. – Geshode Dec 28 '22 at 05:25
  • The line 5 is where I do this: const fs = require('fs'); – P00 Dec 28 '22 at 05:37

1 Answers1

1

fs is a built-in module in Node.js. You just have to require it, like this:

const fs = require('fs');

and then you can use it.

The fact, that you have fs in your package.json might interfere with the built-in module. You should remove fs from your package.json and run npm install to remove any packages, which might have been unnecessarily installed.

After that it should work.

Geshode
  • 3,600
  • 6
  • 18
  • 32
  • I've removed node_modules and fs from the package.json and run npm install adding the line that you told me and the error is the same. Error: Can't resolve 'fs'. Meaby has something to do with the node version? I have the 14.21.1 – P00 Dec 28 '22 at 06:08