SyntaxError: Unexpected token { at Module._compile - javascript

Here's my code for 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",function(){
return Election.deployed().then(function(instance){
return instance.candidatesCount();
}).then(function(count){
AssertionError.equal(count,2);
});
});
});
When I am trying to test it using:
truffle test
The following error is displaying at the terminal:
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
Please help me in dealing with this error.
Thanks in advance.

Related

Next.js error: Unexpected token 'export' for next/server.js

I am getting Unexpected token 'export' when trying to yarn dev my Nextjs project. Here is the error message I am getting (after running yarn build).
> Build error occurred
/Users/.../my-repo/node_modules/next/server.js:1
export { NextRequest } from 'next/dist/server/web/spec-extension/request'
^^^^^^
SyntaxError: Unexpected token 'export'
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/.../my-repo/node_modules/#clerk/nextjs/dist/server/utils.js:5:18)
at Module._compile (node:internal/modules/cjs/loader:1101:14) {
type: 'SyntaxError'
}
This is what node_modules/next/server.js looks like in my project.
export { NextRequest } from 'next/dist/server/web/spec-extension/request'
export { NextResponse } from 'next/dist/server/web/spec-extension/response'
Also, here is next.config.js:
/** #type {import('next').NextConfig} */
const nextConfig = {
eslint: {
dirs: ["."],
},
poweredByHeader: false,
trailingSlash: true,
basePath: "",
env: {
PROJECT_NAME: process.env.PROJECT_NAME,
},
reactStrictMode: true,
};
const withBundleAnalyzer = require("#next/bundle-analyzer")({
enabled: process.env.ANALYZE === "true",
});
module.exports = withBundleAnalyzer(nextConfig);
I have already tried using next-transpile-modules, but this did not help. For reference, I am (and need to be) using v12.1.0 of next, so I can't take advantage of the in-built transpile support that v13.1 comes with.
What could the problem be?
I think you're importing some components but you're using wrong syntax!
You need to change export { NextRequest } from 'next/dist/server/web/spec-extension/request' to import { NextRequest } from 'next/dist/server/web/spec-extension/request' .
Please Check the Documentation for Further Information on NextJS Docs.

in a simple "hello world" jest implementation, I get this error: SyntaxError: Unexpected token '.'

I am following the documentation at https://jestjs.io/docs/getting-started
I have a directory structure like...
learnjest/sum.js
learnjest/sum.test.js
learnjest/package.json
learnjest/package-lock.json
learnjest/node_modules
My sum.js, sum.test.js and package.json are all exact copies of the code in the "Getting Started" example.
sum.js:
function sum(a, b) {
return a + b;
}
module.exports = sum;
sum.test.js:
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
package.json:
{
"scripts": {
"test": "jest"
}
}
When I run npm test, I see...
> # test /path/to/learnjest
> jest
/path/to/learnjest/node_modules/jest/node_modules/jest-cli/build/cli/index.js:161
if (error?.stack) {
^
SyntaxError: Unexpected token '.'
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/path/to/learnjest/node_modules/jest/node_modules/jest-cli/build/index.js:13:12)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
npm ERR! Test failed. See above for more details.
__drush_ps1: command not found
__git_ps1: command not found
The problem can occur if you are using an older version of Node.js. For example, I had the same problem with version 12. After I upgraded Node.js version to 19.2 the error was fixed.

Discord.js v12: Error: The class/prototype returned from the extender function must extend the existing structure class/prototype

I am trying to set up a event handler for my bot. After I add the code and start the bot it outputs the error:
Error:
Error: The class/prototype returned from the extender function must extend the existing structure class/prototype (received function ExtendedTextChannel extends TextChannel; expected extension of ExtendedTextChannel).
at Function.extend (/home/runner/ricebot/node_modules/discord.js/src/util/Structures.js:82:13)
at module.exports (/home/runner/ricebot/node_modules/discord-buttons/src/index.js:24:16)
at Object.<anonymous> (/home/runner/ricebot/index.js:35:27)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/home/runner/ricebot/events/ready.js:1:16)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at /home/runner/ricebot/index.js:119:18
Code:
readdirSync("./events/").forEach((file) => {
const events = readdirSync("./events/").filter((file) =>
file.endsWith(".js")
);
for(let file of events) {
let pull = require(`./events/${file}`);
if(pull.name) {
client.events.set(pull.name, pull);
} else {
continue;
}
}
});
The error log leads me to this line 35
require('discord-buttons')(client); and to this line 119 let pull = require(`./events/${file}`)
This really seems confusing to me. Thanks in advance.

TypeError: generateKeyPairSync not a function

I am trying to use the generateKeyPairSync() function of the crypto module of node.js. But am getting a typerError saying that there is no such function.
Its strange because another function of the same module createCipher() works perfectly!
Here is the sample code:
import * as cryp from 'crypto';
export function generate_keys() {
console.log('hey there!');
const keyPair = cryp.generateKeyPairSync('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: 'top secret'
}
});
console.log(keyPair);
}
function encrypt(data: string) {
let cipher = cryp.createCipher('aes-256-ecb', 'pass');
cipher.update(data, 'utf8');
return cipher.final('hex');
}
// This is working
console.log(encrypt('asa'));
// Getting error - 'generateKeyPairSync is not a function'
generate_keys();
This is the complete console output:
[Running] ts-node "d:\projects\myProject\src\ply.ts"
4eb35bc94bd4d17fad4857b17558b2ad
hey there!
d:\projects\myProject\src\ply.ts:5
const keyPair = cryp.generateKeyPairSync('rsa', {
^
TypeError: cryp.generateKeyPairSync is not a function
at generate_keys (d:\projects\myProject\src\ply.ts:5:24)
at Object.<anonymous> (d:\projects\myProject\src\ply.ts:33:1)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Module.m._compile (C:\Users\anmolsingh.jaggi\AppData\Local\Yarn\Data\global\node_modules\ts-node\src\index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Object.require.extensions.(anonymous function) [as .ts] (C:\Users\anmolsingh.jaggi\AppData\Local\Yarn\Data\global\node_modules\ts-node\src\index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
[Done] exited with code=1 in 2.097 seconds
From the docs:
Added in: v10.12.0
I guess you are running an earlier version of node

Unable to import Reactjs Class for testing with mocha and enzyme

My test file's code is:
var React = require('react');
var {shalow} = require('enzyme')
var {SearchBox} = require('../static/js/functions')
var expect = require('expect');
describe('Test', function(){
it('1', function(){
expect(true).toEqual(true);
});
});
This is my React class in functions.js
var SearchBox = React.createClass({
render: function() {
return (
<div>
<SearchList data={this.props.data}></SearchList>
<li>
<button id="previous_page" className="previous_page" onClick={back}>Previous</button>
<button id="next_page" className="next_page" onClick={next}>Next</button>
</li>
</div>
)
}
});
It is working fine but I want to write tests and when I run it, it outputs the following error :
irtza#irtza-Lenovo-G50-70:~/Desktop/Kamal Hasan/pedialink$ mocha
./js_test/*.js
/home/irtza/Desktop/Kamal Hasan/pedialink/static/js/functions.js:39
,
^ SyntaxError: Unexpected token <
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:511:25)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Module.require (module.js:466:17)
at require (internal/module.js:20:19)
at Object. (/home/irtza/Desktop/Kamal Hasan/pedialink/js_test/test.js:3:19)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Module.require (module.js:466:17)
at require (internal/module.js:20:19)
at /usr/local/lib/node_modules/mocha/lib/mocha.js:220:27
at Array.forEach (native)
at Mocha.loadFiles (/usr/local/lib/node_modules/mocha/lib/mocha.js:217:14)
at Mocha.run (/usr/local/lib/node_modules/mocha/lib/mocha.js:485:10)
at Object. (/usr/local/lib/node_modules/mocha/bin/_mocha:405:18)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Function.Module.runMain (module.js:575:10)
at startup (node.js:159:18)
at node.js:444:3
Your code is a mix between ES5 and ES6 syntax.
I think that the problem come from this portion of code :
var React = require('react');
var {shalow} = require('enzyme');
var {SearchBox} = require('../static/js/functions');
If you wanna use ES5 syntax :
var React = require('react');
var shalow = require('enzyme').shalow; // notice how we access object property `.objectProperty`
var SearchBox = require('../static/js/functions');
If you wanna use ES6 syntax :
import React from 'react';
import {shalow} form 'enzyme'; // notice how we access object property `{objectProperty}`
import SearchBox form '../static/js/functions';
also make sure at the end of your function.js file, you export the component with :
module.exports = SearchBox; // ES5 syntax
export default SearchBox; // ES6 syntax
You need to update Node (mocha is depending on node.js) version (to support es6 modules) or export your component using requireJS - module.exports ... + You should check what parts of es6 your node version supports, because code passed to mocha is not "babelized" :)

Categories

Resources