Javascript SyntaxError:模块处出现意外的标记{。\u编译
以下是我的election.js代码:Javascript SyntaxError:模块处出现意外的标记{。\u编译,javascript,node.js,mocha.js,chai,truffle,Javascript,Node.js,Mocha.js,Chai,Truffle,以下是我的election.js代码: import { isMainThread } from "worker_threads"; import { AssertionError } from "assert"; var Election = artifacts.require("./Election.sol"); contract("Election",function(accounts){ it("initializes with two candidates",functio
import { isMainThread } from "worker_threads";
import { AssertionError } from "assert";
var Election = artifacts.require("./Election.sol");
contract("Election",function(accounts){
it("initializes with two candidates",function(){
return Election.deployed().then(function(instance){
return instance.candidatesCount();
}).then(function(count){
AssertionError.equal(count,2);
});
});
});
当我尝试使用以下工具进行测试时:
truffle test
终端上显示以下错误:
Using network 'development'.
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
/home/shreyas/election/test/election.js:1 import { isMainThread } from "worker_threads";
^
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:718:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
at Module.load (internal/modules/cjs/loader.js:641:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:681:19)
at require (internal/modules/cjs/helpers.js:16:16)
at /usr/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:250:27
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/usr/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:247:14)
at Mocha.run (/usr/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:576:10)
at /usr/lib/node_modules/truffle/build/webpack:/packages/core/lib/test.js:138:1
at new Promise (<anonymous>)
at Object.run (/usr/lib/node_modules/truffle/build/webpack:/packages/core/lib/test.js:137:1)
at processTicksAndRejections (internal/process/task_queues.js:89:5) Truffle v5.1.8 (core: 5.1.8) Node v12.3.1
使用网络“开发”。
编辑您的合同。。。
===========================
>一切都是最新的,没有什么可编译的。
/home/shryas/election/test/election.js:1从“worker_threads”导入{isMainThread};
^
SyntaxError:意外标记{
at模块编译(内部/modules/cjs/loader.js:718:23)
at Object.Module._extensions..js(internal/modules/cjs/loader.js:785:10)
在Module.load(内部/modules/cjs/loader.js:641:32)
at Function.Module._load(内部/modules/cjs/loader.js:556:12)
at Module.require(内部/modules/cjs/loader.js:681:19)
根据需要(内部/modules/cjs/helpers.js:16:16)
at/usr/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:250:27
在Array.forEach()处
在Mocha.loadFiles(/usr/lib/node_modules/truffle/node_modules/Mocha/lib/Mocha.js:247:14)
在Mocha.run(/usr/lib/node_modules/truffle/node_modules/Mocha/lib/Mocha.js:576:10)
at/usr/lib/node_modules/truffle/build/webpack:/packages/core/lib/test.js:138:1
在新的承诺()
在Object.run(/usr/lib/node_modules/truffle/build/webpack:/packages/core/lib/test.js:137:1)
在processTicksAndRejections(internal/process/task_queues.js:89:5)Truffle v5.1.8(核心:5.1.8)节点v12.3.1
请帮我处理这个错误。
提前感谢。正在加载的
election.js
不是作为ESM模块加载的,因此它不知道导入{意味着什么。或者告诉它正在处理ESM模块(如果它可以处理它们),或改用Node.js的旧式CJS模块。import
关键字在js文件中的语法无效。Node将您的文件解释为js文件(提示提及CJS,它是对Node.js原始模块系统common js的引用)因此,使用import
关键字是一个语法错误。只有在模块文件(mjs文件)中才支持import
关键字事实上,我是一个处理这个问题的新手。@T.J.Crowder你能告诉我应该做什么修改吗?或者你?@slebetmanWhatever正在加载election.js
不是作为ESM模块加载的,所以它不知道import{
意味着什么。或者告诉它正在处理ESM模块(如果它可以处理它们的话),或改用Node.js的旧式CJS模块。import
关键字在js文件中的语法无效。Node将您的文件解释为js文件(提示提及CJS,它是对Node.js原始模块系统common js的引用)因此,使用import
关键字是一个语法错误。import
关键字仅在模块文件(mjs文件)中受支持。实际上,我是一个处理此问题的初学者。@T.J.Crowder您能告诉我应该做什么修改吗?还是您?@slebetman