node.js in frontend with webpack. ReferenceError: require is not defined at fs/promises. @trustwallet/wallet-core issue

40 views Asked by At

I updated @trustwallet/wallet-core in my node.js project and ran into a problem with the require fs/promises. Does anyone have a solution to this problem? I will be grateful for any help. Error: problem`

bundl.js generation: bundl.js generation

Files: //package.json

{
  "name": "crypto-lib",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "keywords": [],
  "author": "",
  "license": "ISC",
  "watch": {
    "build-dev": "*.js"
  },
  "scripts": {
    "clean": "rm dist/bundle.js",
    "build-dev": "webpack --mode development",
    "build-prod": "webpack --mode production ",
    "watch": "npm-watch"
  },
  "private": true,
  "dependencies": {
    "@trustwallet/wallet-core": "4.0.18",
    "bn.js": "^5.2.1",
    "bs58": "^5.0.0",
    "buffer": "^6.0.3",
    "fs": "npm:[email protected]",
    "fs-extra": "^11.2.0",
    "i": "^0.3.7",
    "long": "5.2.3",
    "near-api-js": "^3.0.2",
    "node-polyfill-webpack-plugin": "^3.0.0",
    "npm": "^10.3.0",
    "npm-watch": "^0.11.0",
    "path-browserify": "^1.0.1",
    "protobufjs": "^7.2.6",
    "tweetnacl": "^1.0.3",
    "web-vitals": "^3.5.1"
  },
  "devDependencies": {
    "copy-webpack-plugin": "^12.0.2",
    "webpack": "^5.89.0",
    "webpack-cli": "^5.1.4"
  }
}

//webpack.config.js

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

const config = {
  entry: "./src/index.js",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.js",
    libraryTarget: "commonjs2",
  },
  target: "node",
  devtool: "inline-source-map", // This option controls if and how source maps are generated.
  module: {
    rules: [
      {
        parser: {
          // commonjs: false, // disable CommonJS
          // system: false, // disable SystemJS
          // harmony: false, // disable ES2015 Harmony import/export
          // requireInclude: false, // disable require.include
          // requireEnsure: false, // disable require.ensure
          // requireContext: true, // disable require.context
          browserify: true, // disable special handling of Browserify bundles
          // requireJs: true, // disable requirejs.*
          node: true, // disable __dirname, __filename, module, require.extensions, require.main, etc.
          // commonjsMagicComments: false, // disable magic comments support for CommonJS
          // node: {}, // reconfigure node layer on module level
          // worker: ["default from web-worker", "..."], // Customize the WebWorker handling for javascript files, "..." refers to the defaults.
        },
      },
    ],
  },
  plugins: [
    new NodePolyfillPlugin({
      excludeAliases: ["console"],
    }),
    new CopyWebpackPlugin({
      patterns: [{ from: "static", to: "" }],
    }),
  ],
};

module.exports = config;

//index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Crypto-lib</title>
    <meta charset="utf-8" />
  </head>
  <body>
    <div id="app"></div>
    <script src="bundle.js"></script>
  </body>
</html>

I'have tried to use "node-polyfill-webpack-plugin" which eliminates this error but generates new one: ReferenceError: require is not defined at @trustwallet/wallet-core

0

There are 0 answers