Search code examples
javascriptcompilationethereumsolidity

TypeError: Cannot read property 'compile' of undefined


I'm currently trying to follow this tutorial for Ethereum Solidity coding, and for the following code:

const path = require('path');
const fs =  require('fs');        // File system module
const solc = require('solc').default;    // Solidity Compiler module

// Note that phrase resolving a link means to substitute the actual location in the file system for the symbolic link
// If we assume that logFile is a symbolic link to dir/logs/HomeLogFile, then resolving it yields dir/logs/HomeLogFile

// Generates a path that points directly to the inbox file. __dirname will be the root direction
// inboxPath = desktop/inbox/contracts/inbox.sol
const inboxPath = path.resolve(__dirname, 'contracts', 'inbox.sol');

// The next step is to actually read the contents of the source file now
// utf8 is the encoding to read the file's content
const source = fs.readFileSync(inboxPath, 'utf8');

// Call solc.compile and pass in our source code, with only 1 contract 
// console.log() means put the output in the console
console.log(solc.compile(source, 1));

I get the following error when I hover over const solc = require('solc').default:

Could not find a declaration file for module 'solc'. 'c:/Users/Hana PC/Desktop/inbox/node_modules/solc/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/solc` if it exists or add a new declaration (.d.ts) file containing `declare module 'solc';`ts(7016)

When I try node compile.js, I get:

C:\Users\Hana PC\Desktop\inbox\compile.js:18
console.log(solc.compile(source, 1));
                 ^

TypeError: Cannot read property 'compile' of undefined
    at Object.<anonymous> (C:\Users\Hana PC\Desktop\inbox\compile.js:18:18)
[90m    at Module._compile (internal/modules/cjs/loader.js:1063:30)[39m
[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)[39m
[90m    at Module.load (internal/modules/cjs/loader.js:928:32)[39m
[90m    at Function.Module._load (internal/modules/cjs/loader.js:769:14)[39m
[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)[39m
[90m    at internal/main/run_main_module.js:17:47[39m

I've never used/interacted with TypeScript or JavaScript or anything of the sorts, so I honestly don't even know what this error means. I've already tried uninstalling and reinstalling solc multiple times, all to no avail.

My node.js version is:

6.14.8

I tried doing some searching online for what the implicitly has an 'any' type. means or how it could be fixed, but I honestly didn't get any of it. If it helps, my package.json looks like this:

{

"name": "inbox",
  "version": "1.0.0",
  "description": "",
  "main": "compile.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "solc": "^0.8.0"
  }
}

Any help is appreciated!

**** Update:**

Tried it with uninstall solc and a fresh install (without using version 0.4.17), and got an Assertion Error:

assert.js:383
    throw err;
    ^

AssertionError [ERR_ASSERTION]: Invalid callback object specified.
    at runWithCallbacks (C:\Users\Hana PC\Desktop\inbox\node_modules\[4msolc[24m\wrapper.js:97:7)
    at compileStandard (C:\Users\Hana PC\Desktop\inbox\node_modules\[4msolc[24m\wrapper.js:207:14)
    at Object.compileStandardWrapper [as compile] (C:\Users\Hana PC\Desktop\inbox\node_modules\[4msolc[24m\wrapper.js:214:14)

Wish I knew what was goin on


Solution

  • As per the discussion we had above, since the solidity file is using v0.4.17, you should use the same version of solc library in your code.

    I have created a working example of your code here and the only two changes required were:

    • Making sure I use v0.4.17 of solc.
    • Import const solc = require("solc"); and not const solc = require("solc").default;

    If you are having trouble downgrading the solc package, then

    1. Delete package-lock.json file and node_modules/ folder.
    2. Update the package.json files dependency to "solc": "0.4.17" and not to "^0.4.17"
    3. Simply run npm install one last time.

    This should be enough to get you up and running.