TypeError: Cannot read property 'compile' of undefined

TypeError: Cannot read property 'compile' of undefined

我目前正在尝试按照本教程进行以太坊 Solidity 编码和以下代码:

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));

当我将鼠标悬停在 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)

当我尝试 node compile.js 时,我得到:

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

我从来没有 used/interacted 使用过 TypeScript 或 JavaScript 或任何类似的东西,所以老实说,我什至不知道这个错误是什么意思。我已经尝试多次卸载并重新安装 solc,但都无济于事。

我的 node.js 版本是:

6.14.8

我尝试在网上搜索 implicitly has an 'any' type. 的含义或如何修复它,但老实说,我一无所获。如果有帮助,我的 package.json 看起来像这样:

{

"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"
  }
}

感谢任何帮助!

**** 更新:**

通过卸载 solc 和全新安装(不使用版本 0.4.17)进行了尝试,并得到了一个断言错误:

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)

希望我知道发生了什么事

根据我们上面的讨论,由于 solidity 文件使用 v0.4.17,您应该在代码中使用相同版本的 solc 库。

我已经为您的代码创建了一个工作示例 here,仅需进行两处更改:

  • 确保我使用 v0.4.17 的 solc。
  • 导入 const solc = require("solc"); const solc = require("solc").default;

如果您在降级 solc 软件包时遇到问题,那么

  1. 删除 package-lock.json 个文件和 node_modules/ 个文件夹。
  2. package.json 文件依赖更新为 "solc": "0.4.17" 并将 not 更新为 "^0.4.17"
  3. 干脆运行npm install最后一次。

这应该足以让你起床 运行宁。