I'm currently running a Mocha test of my Solidity contract but it throws error which is related to compiler code.
C:\eth\compile.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:\eth\test\inbox.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\node_modules\mocha\lib\esm-utils.js:42:12)
at Object.exports.loadFilesAsync (C:\eth\node_modules\mocha\lib\esm-utils.js:55:34)
at Mocha.loadFilesAsync (C:\eth\node_modules\mocha\lib\mocha.js:473:19)
at singleRun (C:\eth\node_modules\mocha\lib\cli\run-helpers.js:125:15)
at exports.runMocha (C:\eth\node_modules\mocha\lib\cli\run-helpers.js:190:10)
at Object.exports.handler (C:\eth\node_modules\mocha\lib\cli\run.js:362:11)
at C:\eth\node_modules\mocha\node_modules\yargs\build\index.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];
You need to pass the JSON-stringified options object (to the compile() function), not just the text source. See the example in the readme.
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;
Related
An anonymous error in my 'logs' section came on firebase project, while no error was shown on 'run' in Android studio. I don't know anything about node js, i just wanted to link my app through it, that's why i am forced to use it, can someone help me please.
Here's the complete error on firebase project -> Functions -> logs
srv/node_modules/#google-cloud/firestore/build/src/collection-group.js:54
async *getPartitions(desiredPartitionCount) {
^
SyntaxError: Unexpected token *
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/srv/node_modules/#google-cloud/firestore/build/src/index.js:39:28)
Here's the second error on firebase's project -> functions -> logs. :
Error detected in onCreateFollower {"errorEvent":{"eventTime":"2020-10-31T06:41:42.682Z","message":"/srv/node_modules/#google-cloud/firestore/build/src/collection-group.js:54\n async *getPartitions(desiredPartitionCount) {\n ^\n\nSyntaxError: Unexpected token *\n at createScript (vm.js:80:10)\n at Object.runInThisContext (vm.js:139:10)\n at Module._compile (module.js:617:28)\n at Object.Module._extensions..js (module.js:664:10)\n at Module.load (module.js:566:32)\n at tryModuleLoad (module.js:506:12)\n at Function.Module._load (module.js:498:3)\n at Module.require (module.js:597:17)\n at require (internal/module.js:11:18)\n at Object.<anonymous> (/srv/node_modules/#google-cloud/firestore/build/src/index.js:39:28)","serviceContext":{"service":"onCreateFollower","resourceType":"cloud_function"}},"#type":"type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.Insight","errorGroup":"COvaxM_ErLfhbg"
Here's the code in Android Studio on index.js file -
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
// functions.logger.info("Hello logs!", {structuredData: true});
// response.send("Hello from Firebase!");
// });
exports.onCreateFollower = functions.firestore.document("/followers/{userId}/userFollowers/{followerId}").onCreate(async (snapshot, context) => {
console.log('Follower Created', snapshot.id);
const userId = context.params.userId;
const followerId = context.params.followerId;
const followedUserPostsRef = admin.firestore().collection('posts').doc(userId).collection('userPosts');
const timelinePostsRef = admin.firestore().collection('timeline').doc(followerId).collection('timelinePosts');
const querySnapshot = await followedUserPostsRef.get();
querySnapshot.forEach(doc => {
if(doc.exists) {
const postId = doc.id;
const postData = doc.data();
timelinePostsRef.doc(postId).set(postData);
}
})
});
try this "firebase-admin": "^8.13.0","firebase-functions": "^3.6.1". This solved my problem.
Change the version of firebase-admin to "firebase-admin": "^ 8.10.0" and firebase-functions to "firebase-functions": "^ 3.6.1". I also had this problem and solved it.
I am trying to run following code===>
var webdriverio = require('webdriverio');
var options = { desiredCapabilities: { browserName: 'chrome' } };
var client = webdriverio.remote(options);
client
.init()
.url('http://www.webdriveruniversity.com/')
.click('#login-portal')
.getTitle().then(function(title) {
console.log('Title is: ' + title);
})
.end();
The output I am getting as follows. Not sure how to solve it.
const remote = async function (params = {}, remoteModifier) {
^^^^^^^^
SyntaxError: Unexpected token function
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Users\user\Desktop\webdriverFramework\loginPortal
Test.js:1:81)
It seems you are trying to use a library that uses async functions with an old Node.js version that does not support them. Please, run node -v and compare the Node.js version with the async functions support table.
I have recently started Lambda function development on AWS. When try to import a JavaScript file I am getting the following error,
module initialization error: ReferenceError
at Object.<anonymous> (/var/task/model/claim_type.js:3:29)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/task/index.js:2:19)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
In root level I have the index.js file and claim_type.js file inside the model directory.
index.js
const Sequelize = require('sequelize');
const ClaimType = require('./model/claim_type.js');
exports.handler = function (event, context, callback) {
//function content
}
claim_type.js
const Sequelize = require('sequelize');
const ClaimType = sequelize.define('claimtype', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: {
type: Sequelize.STRING
}
}, {
timestamps: false
});
module.exports = ClaimType;
What is the correct way to import the claim_type.js?
const Sequelize = require('sequelize');
const ClaimType = sequelize.define('claimtype', {
your const is Camelcase, but the second line is lowercase!
This Code works on Windows without any errors.
var express = require('express')
, app = express()
, async = require('async')
, bodyParser = require('body-parser')
, cookies = require('cookies')
, cors = require('cors')
, fileUpload = require('multer')
, fs = require('fs')
, moment = require('moment')
, morgan = require('morgan')
, path = require('path')
, session = require('express-session')
, upload = fileUpload({ dest: './uploads' })
, uuid = require('uuid')
;
var connLaw = require('./nodeHelperFunctions/datasources').service
, connPG = require('./nodeHelperFunctions/datasources').postgis
, headers = require('./helperHtml/headers')
, footers = require('./helperHtml/footers')
, passCheck = require('./nodeHelperFunctions/hashsalt')
;
However when the same code is run on an Amazon Linux AMI box, I get the following error.
module.js:471
throw err;
^
Error: Cannot find module './nodeHelperFunctions/datasources'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/ec2-user/testbed/js/app.js:17:59)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
tried changing my code to
var connLaw = require(path.join(__dirname, 'nodeHelperFunctions/datasources')).service
, connPG = require(path.join(__dirname, 'nodeHelperFunctions/datasources')).postgis
, headers = require(path.join(__dirname, 'helperHtml/headers'))
, footers = require(path.join(__dirname, 'helperHtml/footers'))
, passCheck = require(path.join(__dirname, 'nodeHelperFunctions/hashsalt'))
;
again works perfectly in Windows but got a similar error on Linux:
module.js:471
throw err;
^
Error: Cannot find module '/home/ec2-user/testbed/js/nodeHelperFunctions/datasources'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/ec2-user/testbed/js/app.js:17:15)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
how can I get person scripts I have written myself to work on Linux when they work perfecting fine on Windows?
It's hard to tell with what you posted - but requiring modules on linux is case sensitive to the file name, and windows... ...not so. Try checking the case.
edit: not just the filename - but the entire path.
Here is my schema
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var test = new Schema({
name : {
type : String,
require : true,
sparse : true
},
description : {
type : String
},
questions : {
type : [Schema.Types.ObjectId],
sparse : true
} });
module.exports = new mongoose.model('test', test);
When I try to execute, it give me error as follows :
TypeError: Cannot read property 'test' of undefined
at new Mongoose.model (/home/utkarsh/Desktop/MEAN_REST_user_management/node_modules/mongoose/lib/index.js:329:25)
at Object.<anonymous> (/home/utkarsh/Desktop/MEAN_REST_user_management/app/models/test.js:19:18)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/utkarsh/Desktop/MEAN_REST_user_management/app/routes/test.js:6:13)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
I have used the same format earlier, it worked fine. Dont know why its not working. Can anyone help?
Change this:
module.exports = new mongoose.model('test', test);
To this:
module.exports = mongoose.model('test', test);
mongoose.model() isn't a class, so you shouldn't instantiate it (using new).