I finished up an Ethereum smart contract course on Udemy that used solc@^0.4.17, React and Next.js. I thought it would be a fun exercise to try and upgrade everything to the latest version and try to refactor. I have the following code in a file called factory.js being imported into my main index file:
import web3 from './web3';
const path = require('path');
const fs = require('fs');
const abiPath = path.resolve('ethereum/build', 'CampaignFactory.abi');
console.log(abiPath);
const abi = fs.readFileSync(abiPath, 'utf8');
console.log(abi);
const factory = new web3.eth.Contract(
JSON.parse(abi),
'0x2d54559dCe0DA6C92378A916e9eE0422CEFFCD80'
);
export default factory;
Inside of my main index file I'm calling it like so:
import factory from '../ethereum/factory';
...
class CampaignIndex extends Component {
static async getInitialProps() {
const campaigns = await factory.methods.getDeployedCampaigns().call();
return { campaigns };
}
...
Here's the console.log:
wait - compiling /...
event - compiled successfully in 637 ms (1422 modules)
/Users/sonnyparlin/Github/kickstart/ethereum/build/CampaignFactory.abi
[
{
"inputs": [
{
"internalType": "uint256",
"name": "minimum",
"type": "uint256"
}
],
"name": "createCampaign",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "deployedCampaigns",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getDeployedCampaigns",
"outputs": [
{
"internalType": "address[]",
"name": "",
"type": "address[]"
}
],
"stateMutability": "view",
"type": "function"
}
]
The console log confirms that path.resolve() is actually working, but when I go to the page via the web browser I see the following error:

Call Stack
Module../ethereum/factory.js
file:///Users/sonnyparlin/Github/kickstart/.next/static/chunks/pages/index.js (1148:1)
Module.options.factory
/_next/static/chunks/webpack.js (638:31)
__webpack_require__
file:///Users/sonnyparlin/Github/kickstart/.next/static/chunks/webpack.js (37:33)
fn
/_next/static/chunks/webpack.js (307:21)
eval
webpack-internal:///./pages/index.js (9:75)
Module../pages/index.js
file:///Users/sonnyparlin/Github/kickstart/.next/static/chunks/pages/index.js (1192:1)
Module.options.factory
/_next/static/chunks/webpack.js (638:31)
__webpack_require__
file:///Users/sonnyparlin/Github/kickstart/.next/static/chunks/webpack.js (37:33)
fn
/_next/static/chunks/webpack.js (307:21)
eval
node_modules/next/dist/build/webpack/loaders/next-client-pages-loader.js?page=%2F&absolutePagePath=%2FUsers%2Fsonnyparlin%2FGithub%2Fkickstart%2Fpages%2Findex.js! (5:15)
eval
node_modules/next/dist/client/route-loader.js (236:50)
My guess is that this is really some kind of versioning or dependency issue, so I'm also including my package.json file:
{
"name": "kickstart",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha",
"dev": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"@truffle/hdwallet-provider": "^1.0.37",
"fs-extra": "^10.0.0",
"ganache-cli": "^6.1.8",
"mocha": "^9.1.3",
"next": "^12.0.3",
"next-routes": "^1.4.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"semantic-ui-css": "^2.4.1",
"semantic-ui-react": "^2.0.4",
"solc": "^0.8.9",
"web3": "^1.6.0"
},
"browser": {
"fs": false,
"path": false,
"os": false
}
}
And a listing of my project files:

When you import factory.js, next is running entire file, it sees the "path" and it is throwing the error
path is used to get the directory of contract file, and fs is used to read the contract file. Then with solc compiler, you compile this code, you run this file with
node fileName.js
based on instructions you defined with solc, this compilation with create a .json file which has "abi" property. So far we had nothing to do with next.js.
Then, refactor this to a separate file:
import abi from "directory/file.json"
const factory = new web3.eth.Contract(
// pass the abi
JSON.parse(abi),
'0x2d54559dCe0DA6C92378A916e9eE0422CEFFCD80'
);
Now you have to import this into your next.js project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With