类型错误:无法读取未定义的属性“编译”

时间:2021-01-18 19:02:51

标签: javascript compilation ethereum solidity

我目前正在尝试按照本教程进行 Ethereum 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

我从未使用过 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)

希望我知道发生了什么

1 个答案:

答案 0 :(得分:1)

根据我们上面的讨论,由于 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

这应该足以让您开始工作。