1

I have installed solc without any errors or warning, but when i am trying to run node compile.js command in cmd then i am getting following error

enter image description here

On checking the code, i found that "const solc = require('solc');" is returning an error which says

Could not find a declaration file for module 'solc'.

Please help, i am stuck on very first and basic step.

  • 1
    How did u install the compiler? check solidity documentation to see more information about installation. Also check your package.json file to see if the solc compiler exists. – Masoud jt Jun 3 at 4:15
  • I first installed the jason package through nodejs and then compiler using const path = require('path'); const fs = require('fs'); const solc = require('solc'); const inboxPath = path.resolve(__dirname, 'contracts', 'Inbox.sol'); const source = fs.readFileSync(inboxPath, 'utf8'); console.log(solc.compile(source,1)); when i am running the compile.js in nodejs then I am getting the error – Vijender Singh Jun 4 at 5:12
  • I have 3 questions. Are you studying the udemy course (the complete developers guide) ? What version of solidity are you using ? what version of solc did u install? – Masoud jt Jun 4 at 5:17
  • 1. Yes, still using it but finding it difficult since they are not updating the content with update version of packages and how to use it... Would be great if you have better suggestion which you can share with me. 2. Using Visual Studio as a text editor for solidity and it has latest version 0.0.73 3. Solc version is also latest 0.6.8 – Vijender Singh Jun 8 at 9:23
  • I really recommend you to use remix text editor for you solidity code. That course is good but you must keep yourself up to date by searching about every error you encounter, which is a good practice for you to learn better. now i'm gonna give you the updated code in the answer section. – Masoud jt Jun 8 at 12:15
1

Follow every step in the course. but try to install the updated version of everything you see on that course. To install solc just type npm install solc in the nodejs command prompt, this will install the latest version automatically ( you don't need to specify the version ).

You can also try uninstalling and reinstalling the solc by doing npm uninstall solc first, then again install it by npm install solc.

For the code part, these are the updated codes you need for the course.

Please just don't copy and paste, make sure you learn from the changes.

Inbox.sol:

pragma solidity ^0.6.6;

contract Inbox{
    string public message;

    constructor (string memory initialMessage) public{
        message = initialMessage;
    }

    function setMessage(string memory newMessage) public{
        message = newMessage;
    }
}

Compile.js

const path = require('path');
const fs = require('fs');
const solc = require('solc');

const inboxpath = path.resolve(__dirname, 'Contracts', 'Inbox.sol');
const source = fs.readFileSync(inboxpath, 'UTF-8');

var input = {
    language: 'Solidity',
    sources: {
        'Inbox.sol' : {
            content: source
        }
    },
    settings: {
        outputSelection: {
            '*': {
                '*': [ '*' ]
            }
        }
    }
};

var output = JSON.parse(solc.compile(JSON.stringify(input)));

exports.abi = output.contracts['Inbox.sol']['Inbox'].abi;
exports.bytecode = output.contracts['Inbox.sol']['Inbox'].evm.bytecode.object;

Deploy.js

const HDWalletProvider = require('truffle-hdwallet-provider');
const Web3 = require('web3');
const { abi, bytecode} = require('./compile');

const provider = new HDWalletProvider(
  'Your mnemonic',
  'https://rinkeby.infura.io/...'
);
const web3 = new Web3(provider);

const deploy = async () => {
  const accounts = await web3.eth.getAccounts();

  console.log('Attempting to deploy from account',accounts[0]);

  const result = await new web3.eth.Contract(abi)
    .deploy({data: '0x' + bytecode, arguments: ['Hi there']})
    .send({from: accounts[0]});

  console.log('contract deployed to', result.options.address);

}
deploy();

Please just don't copy and paste, make sure you learn from the changes.

| improve this answer | |
  • Thanks for your prompt response. – Vijender Singh Jun 9 at 12:24
  • Your welcome. Was it helpful? – Masoud jt Jun 9 at 12:27
  • Still getting the same error.... but i appreciate your response on this... Could you suggest the way forward to learn and master blockchain technology.. I know how it works... and selected Ethereum - solidity for development.... but don't know the approach to take .... – Vijender Singh Jun 18 at 5:57
  • I'm also a newbie in this field and I'm still trying to learn to get better in this. I think watching different courses on Udemy or Youtube is a good way to start. Then I think it's better to read the solidity and web3js documentation. Then find and read some code examples from experts. Then continue your journey by learning front-end UI coding and code your own Smart Contract. I think this is a good approach. (Also don't forget to read articles). – Masoud jt Jun 19 at 14:12
  • Thanks for your guidance – Vijender Singh Jun 23 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.