为什么我不能运行 node compile.js?

问题描述

我为正在编写的智能合约编写了一个 compile.js 文件(出于教育目的),但是当我使用 node compile.js 命令运行它时,我收到一个错误消息,显示 ReferenceError: compile is not defined

我在 Sublime Text 3 (windows 10) 中使用了 solidity ^0.4.17

这是它的样子:

C:\Users\zakja\Desktop\inBox>node compile.js
C:\Users\zakja\Desktop\inBox\compile.js:1
compile.js
^

ReferenceError: compile is not defined
    at Object.<anonymous> (C:\Users\zakja\Desktop\inBox\compile.js:1:1)
←[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

下面是compile.js文件

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,'utf8');


console.log(solc.compile(source,1));


Any help is appreciated. (Note: Newbie here)

解决方法

第一行是你的文件是'compile.js'文件的名字。如果是,则删除第一行。您不需要文件名作为文件中的内容。删除第一行并运行。