Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

node.js - Unexpected token '[' when running Solidity test

I'm currently running a Mocha test of my Solidity contract but it throws error which is related to compiler code.

C:ethcompile.js:8
modules.exports = solc.compile(source).[];

SyntaxError: Unexpected token '['
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (C:ethestinbox.test.js:5:31)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.exports.requireOrImport (C:eth
ode_modulesmochalibesm-utils.js:42:12)
    at Object.exports.loadFilesAsync (C:eth
ode_modulesmochalibesm-utils.js:55:34)
    at Mocha.loadFilesAsync (C:eth
ode_modulesmochalibmocha.js:473:19)
    at singleRun (C:eth
ode_modulesmochalibcli
un-helpers.js:125:15)
    at exports.runMocha (C:eth
ode_modulesmochalibcli
un-helpers.js:190:10)
    at Object.exports.handler (C:eth
ode_modulesmochalibcli
un.js:362:11)
    at C:eth
ode_modulesmocha
ode_modulesyargsuildindex.cjs:443:71

The compiler itself is:

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

modules.exports = solc.compile(source).[:Inbox];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. You need to pass the JSON-stringified options object (to the compile() function), not just the text source. See the example in the readme.
  2. Your solc.compile(source).[:Inbox] code has syntax errors (combining access to property with . and array with [) and logical errors (undefined :Inbox, incorrect path to the compiled result, trying to access a JSON string as an object).

Asssuming that inbox.sol contains contract Inbox that is the main contract you want to compile, this is a working code replacement for the last line in your question:

const options = {
  language: 'Solidity',
  sources: {
    'inbox.sol': {
      content: source
    }
  },
  settings: {
    outputSelection: {
      '*': {
        '*': ['*']
      }
    }
  }
};
const compiledRaw = solc.compile(JSON.stringify(options));
const compiledObj = JSON.parse(compiledRaw);
const compiledInboxContract = compiledObj.contracts['inbox.sol']['Inbox'];
//console.log(compiledInboxContract)
module.exports = compiledInboxContract;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...