Web3 broken dependency BufferUtil.js, Validation.js scrypt, websocket
Expected behavior
The module need to compile
Actual behavior
external_"events":1 Uncaught ReferenceError: require is not defined
at eval (external_"events":1)
at Object.events (main.js:5145)
at __webpack_require__ (main.js:20)
at eval (utils.js:10)
at Object../node_modules/@web3-js/websocket/lib/utils.js (main.js:272)
at __webpack_require__ (main.js:20)
at eval (WebSocketServer.js:17)
at Object../node_modules/@web3-js/websocket/lib/WebSocketServer.js (main.js:261)
at __webpack_require__ (main.js:20)
at eval (websocket.js:2)
:8080/favicon.ico:1 Failed to load resource: the server responded with a status of 404 (Not Found)
Steps to reproduce the behaviour
- [First step] I am using the web pack, created Index. js
- [Second step] var Web3 = require("web3");
- [and so on...] when npm run build
Logs
WARNING in ./node_modules/@web3-js/websocket/lib/BufferUtil.js
Module not found: Error: Can't resolve '../build/Release/bufferutil' in '/node_modules/@web3-js/websocket/lib'
@ ./node_modules/@web3-js/websocket/lib/BufferUtil.js
@ ./node_modules/@web3-js/websocket/lib/WebSocketFrame.js
@ ./node_modules/@web3-js/websocket/lib/websocket.js
@ ./node_modules/@web3-js/websocket/index.js
@ ./node_modules/web3-providers-ws/src/index.js
@ ./node_modules/web3-core-requestmanager/src/index.js
@ ./node_modules/web3-core/src/index.js
@ ./node_modules/web3/src/index.js
@ ./src/web.js
@ ./src/index.js
WARNING in ./node_modules/@web3-js/websocket/lib/Validation.js
Module not found: Error: Can't resolve '../build/Release/validation' in '/node_modules/@web3-js/websocket/lib'
@ ./node_modules/@web3-js/websocket/lib/Validation.js
@ ./node_modules/@web3-js/websocket/lib/WebSocketConnection.js
@ ./node_modules/@web3-js/websocket/lib/websocket.js
@ ./node_modules/@web3-js/websocket/index.js
@ ./node_modules/web3-providers-ws/src/index.js
@ ./node_modules/web3-core-requestmanager/src/index.js
@ ./node_modules/web3-core/src/index.js
@ ./node_modules/web3/src/index.js
@ ./src/web.js
@ ./src/index.js
WARNING in ./node_modules/@web3-js/websocket/lib/BufferUtil.js
Module not found: Error: Can't resolve '../build/default/bufferutil' in '/node_modules/@web3-js/websocket/lib'
@ ./node_modules/@web3-js/websocket/lib/BufferUtil.js
@ ./node_modules/@web3-js/websocket/lib/WebSocketFrame.js
@ ./node_modules/@web3-js/websocket/lib/websocket.js
@ ./node_modules/@web3-js/websocket/index.js
@ ./node_modules/web3-providers-ws/src/index.js
@ ./node_modules/web3-core-requestmanager/src/index.js
@ ./node_modules/web3-core/src/index.js
@ ./node_modules/web3/src/index.js
@ ./src/web.js
@ ./src/index.js
WARNING in ./node_modules/@web3-js/websocket/lib/Validation.js
Module not found: Error: Can't resolve '../build/default/validation' in '/node_modules/@web3-js/websocket/lib'
@ ./node_modules/@web3-js/websocket/lib/Validation.js
@ ./node_modules/@web3-js/websocket/lib/WebSocketConnection.js
@ ./node_modules/@web3-js/websocket/lib/websocket.js
@ ./node_modules/@web3-js/websocket/index.js
@ ./node_modules/web3-providers-ws/src/index.js
@ ./node_modules/web3-core-requestmanager/src/index.js
@ ./node_modules/web3-core/src/index.js
@ ./node_modules/web3/src/index.js
@ ./src/web.js
@ ./src/index.js
WARNING in ./node_modules/@web3-js/scrypt-shim/src/index.js
Module not found: Error: Can't resolve 'scrypt' in '/node_modules/@web3-js/scrypt-shim/src'
@ ./node_modules/@web3-js/scrypt-shim/src/index.js
@ ./node_modules/web3-eth-accounts/src/index.js
@ ./node_modules/web3-eth/src/index.js
@ ./node_modules/web3/src/index.js
@ ./src/web.js
@ ./src/index.js
WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/
Versions
[NPM:'6.14.2',
Node:v12.16.1,
Web3.js:1.2.6,
OS MacOs :10.15.3
]
@Nagaraj007
Using a minimal example webpack compiles Web3 without difficulty. There's probably something in your webpack configuration that is the source of the problems you're seeing.
Sys
OSX: 10.13.1
Node: 12.16.0
Web3: 1.2.6
package.json
{
"name": "web3-webpack",
"version": "1.0.0",
"scripts": {
"build": "webpack --config ./webpack.config.js --mode development --output ./dist/main.js",
"build:prod": "webpack --config ./webpack.config.js --mode production --output ./dist/main.js"
},
"devDependencies": {
"webpack": "^4.42.0",
"webpack-cli": "^3.3.11"
},
"dependencies": {
"web3": "1.2.6"
}
}
webpack.config.js
module.exports={}
src/index.js
var Web3 = require('web3');
var web3 = new Web3();
Output of npm run build
> webpack --config ./webpack.config.js --mode development --output ./dist/main.js
Hash: 9895806562c0acc029e0
Version: webpack 4.42.0
Time: 1948ms
Built at: 03/15/2020 9:22:33 AM
Asset Size Chunks Chunk Names
main.js 2.09 MiB main [emitted] main
Entrypoint main = main.js
[0] buffer (ignored) 15 bytes {main} [optional] [built]
[1] util (ignored) 15 bytes {main} [built]
[2] util (ignored) 15 bytes {main} [built]
[3] crypto (ignored) 15 bytes {main} [optional] [built]
[./node_modules/webpack/buildin/amd-options.js] (webpack)/buildin/amd-options.js 80 bytes {main} [built]
[./node_modules/webpack/buildin/global.js] (webpack)/buildin/global.js 472 bytes {main} [built]
[./node_modules/webpack/buildin/module.js] (webpack)/buildin/module.js 497 bytes {main} [built]
[./src/index.js] 70 bytes {main} [built]
+ 334 hidden modules
Closing, please ping if the basic config above doesn't compile on your machine for some reason.
@CGEWECKE, I tried the basic web3 with web-pack with the above configuration, it works well, but when I use the below config, it shows the same error as I mentioned above. I want to use target: node because want to read the file, So I need to use var fs = require('fs')
module.exports = {
target: 'node',
}
@Nagaraj007 Ah! You may have to resolve some of the dependencies manually. An example of an Ethereum project that webpacks for Node is Truffle. Their config is here.
For example, in webpack.config.js they have...
resolve: {
alias: {
"ws": path.join(__dirname, "./nil.js"),
"bn.js": path.join(
__dirname,
"../..",
"node_modules",
"bn.js",
"lib",
"bn.js"
),
"original-fs": path.join(__dirname, "./nil.js"),
"scrypt": "js-scrypt"
}
},
Still, I am facing the same issue, if I try to use the above code with target:node
Uncaught ReferenceError: require is not defined
at eval (external_"events":1)
at Object.events (main.js:4812)
at __webpack_require__ (main.js:20)
at eval (utils.js:10)
at Object../node_modules/websocket/lib/utils.js (main.js:4463)
at __webpack_require__ (main.js:20)
at eval (WebSocketServer.js:17)
at Object../node_modules/websocket/lib/WebSocketServer.js (main.js:4452)
at __webpack_require__ (main.js:20)
at eval (websocket.js:2)
@Nagaraj007 Out of curiosity are you writing a Node application? Or something for the browser?
I trying to build node js as server and browser for UI