2

I'm trying to run truffle migrate on two js files: 1_initial_migration.js and 2_deploy_contracts.js. I can successfully compile my .sol files to .json ABIs but then when I try to migrate I get the following error:

const Migrations = artifacts.require("Migrations");
                             ^

TypeError: Cannot read property 'require' of undefined

Here's how I've utilised artifacts in my js files:

const { artifacts } = require("truffle");
const Migrations = artifacts.require("Migrations");

truffle version results are as follows:

Truffle v5.1.39 (core: 5.1.39)
Solidity v0.5.16 (solc-js)
Node v14.16.0
Web3.js v1.2.1

Also I'm following this course on youtube.

I've seen a couple of posts about changing solitidy version, solc(?) version, and truffle version. I've tried downgrading my global truffle version to 5.1.39 and upgrading the solidity version at the start of my .sol files to ^0.6.0, as that seems to be the recommendations from those posts:

https://ethereum.stackexchange.com/questions/84388/solidity-0-6-0-truffle-compile-error-cannot-read-property-of-undefined

https://github.com/trufflesuite/truffle/issues/4191

crevulus
  • 1,658
  • 12
  • 42
  • I am also facing same issue in my project. Can you please share the solution if you have found it? – coder_newbie Jan 05 '22 at 18:53
  • @coder_newbie I still haven't found a solution, but I have restarted with the same youtuber but a different video: https://www.youtube.com/watch?v=xWFba_9QYmc This time I didn't run into the same issue. No idea why. The only difference is that I didn't try using `require` first - just ran `truffle migrate` while my IDE threw linting errors. – crevulus Jan 06 '22 at 20:03
  • Can you please explain in more detail . what exactly do you mean by `I didn't try using require first`. And are you getting the same output as the youtuber? – coder_newbie Jan 08 '22 at 10:38
  • 1
    Yes, I'm getting the same output as the youtuber (in the second video I linked in my comment). In my post above I mentioned that I included `const {artifacts} = require("truffle")`. In this latest attempt, I skipped that step. My IDE doesn't recognise where `artifacts` is coming from, but it still works because truffle is working some magic in the background. In short: try following the second video, see how you get on. – crevulus Jan 08 '22 at 17:59

2 Answers2

0

Try these files:

1_initial_migration.js:

const Migrations = artifacts.require("Migrations");

module.exports = function(deployer) {
  deployer.deploy(Migrations);
};

2_deploy_contracts.js:

const YourContractName = artifacts.require("YourContractName");

module.exports = function(deployer) {
  deployer.deploy(YourContractName);
};
Silvio Guedes
  • 1,134
  • 15
  • 16
0

Don't do anything just update the truffle Your error will be removed

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 15 '22 at 15:37