Electron - Can't initialize program - javascript

I followed a few video tutorials (such as this one and this one), which use a very similar code, which throws up an error.
My files:
package.json
{
"name": "steamcmd-gui",
"version": "0.0.1",
"author": "avi12",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"dependencies": {
"electron": "^1.7.5"
}
}
main.js
const electron = require("electron");
const url = require("url");
const path = require("path");
let win;
function createWindow() {
win = new electron.BrowserWindow();
win.loadURL(url.format({
pathname: path.join(__dirname, "index.html"),
protocol: "file",
slashes: true
});
);
win.on("closed", () => win = null);
electron.app.on("ready", createWindow);
electron.app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
When trying to execute main.js, the debugger throws:
TypeError: Cannot read property 'on' of undefined
at Object.<anonymous> (C:\Users\avi12\OneDrive\Documents\WebStorm\SteamCMD GUI\main.js:18:13)
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.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3
Which means that electron.app is undefined for some reason.
Thanks a lot for helping!

Thanks to Fast Snail, who realized that I should execute my main.js using electron main.js - I looked up in Google for a solution and found this very nice tutorial, which solved my problem.
The thing was that I had to specify some extra configurations in order to execute my script using Electron, from JetBrains WebStorm.
Thanks a lot!

Related

ReferenceError: require is not defined (at node:internal/main/run_main_module:23:47) in Node.js

I am learning Node.js (Node.js v18.12.1) and started building my first server (backend app).
here is the code
const express = requrie("express");
const app = express();
const port = 3000;
app.get("/", function (req, res) {
res.sendFile("index.html");
});
app.listen(port, function () {
console.log("Server is running on 3000");
});
However, I am getting the following error when I provide the command node app.js in terminal
const express = requrie("express");
^
ReferenceError: requrie is not defined
at Object.<anonymous> (C:\Users\harsh\OneDrive\Documents\Visual Studio 2022\Web Development\firstNodeServer\app.js:3:17)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47
Also this is my package.json
{
"name": "firstnodeserver",
"version": "1.0.0",
"description": "This is my First Node Server",
"main": "app.js",
"type": "commonjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Harshad Tekwani",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"jshint": "^2.13.6",
"nodemon": "^2.0.20"
}
}
Please help me with respect to resolving this error.
I was trying to run the server at localhost:3000 which should have given me on my home route the index.html but instead I am getting the above error.
require is Correct Not requrie

TypeError: Cannot read property 'HttpProvider' of undefined while deploying solidity contract using nodejs

I have below nodejs code to deploy smart contract from a file into local private ethereum network.
const fs = require('fs');
const Web3 = require('web3');
const solc = require('solc');
const web3 = new Web3();
web3.setProvider(web3.providers.HttpProvider('http://localhost:8545'));
const address = web3.eth.accounts[0];
const code = fs.readFileSync('../Calculator.sol').toString()
const compiledCode = solc.compile(code)
const abiDefinition = JSON.parse(compiledCode.contracts[':Calculator'].interface)
const byteCode = compiledCode.contracts[':Calculator'].bytecode
const CalcContract = web3.eth.contract(abiDefinition)
I've below dependencies in package.json
"dependencies": {
"ethereumjs-testrpc": "^6.0.3",
"fs": "0.0.1-security",
"solc": "^0.5.9",
"web3": "^1.0.0-beta.55"
}
When I run the program I get below error at line web3.setProvider(web3.providers.HttpProvider('http://localhost:8545'));
[raj#localhost first_truffle_project]$ sudo node js/example_deployer.js
/home/raj/Coding/Ethereum/first_truffle_project/js/example_deployer.js:6
web3.setProvider(web3.providers.HttpProvider('http://localhost:8545'));
^
TypeError: Cannot read property 'HttpProvider' of undefined
at Object.<anonymous> (/home/raj/Coding/Ethereum/first_truffle_project/js/example_deployer.js:6:33)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
What is wrong with my program? How can I resolve this error?
Try like this :
web3 = new Web3(new Web3.providers.HttpProvider('node-url'))
Make sure your Web 3 version in package.json is
"web3": "^1.0.0-beta.37"
an I believe you want something like this. (Note the capitalization.)
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));

Electronjs "App threw an error during load, ReferenceError: require is not defined"

Developing an application using electron but I'm getting error when I run npm start:
$ npm start
> test#1.0.0 start C:\Users\jakeh\OneDrive\Documents\Coding\Electron Apps\Test
> electron .
App threw an error during load:
ReferenceError: reauire is not defined
at Object.<anonymous> (C:\Users\jakeh\OneDrive\Documents\Coding\Electron Apps\Test\main.js:3:14)
at Module._compile (internal/modules/cjs/loader.js:808:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:820:10)
at Module.load (internal/modules/cjs/loader.js:677:32)
at tryModuleLoad (internal/modules/cjs/loader.js:609:12)
at Function.Module._load (internal/modules/cjs/loader.js:601:3)
at loadApplicationPackage (C:\Users\jakeh\OneDrive\Documents\Coding\Electron Apps\Test\node_modules\electron\dist\resources\default_app.asar\main.js:105:12)
at Object.<anonymous> (C:\Users\jakeh\OneDrive\Documents\Coding\Electron Apps\Test\node_modules\electron\dist\resources\default_app.asar\main.js:153:5)
at Module._compile (internal/modules/cjs/loader.js:808:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:820:10)
I tried adding:
mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true
}
});
Still no luck.
const electron = require('electron');
const url = require('url');
const path = reauire('path');
const {app, BrowserWindow} = electron;
let mainWindow;
// Listen for app to be ready
app.on('ready', function(){
// Create new window
mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true
}
});
// Load html into window
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'mainWindow.html'),
protocol:'file:',
slashes: true
}));
});
Screen shot of error: http://prntscr.com/o32uon
Jake:
Just another passerby: is it possible that you meant to say
const path = require ('path');
and this is a simple syntax issue?
Fixed the issue, was a typo with an import.

Syntax Error: Unexpected Token async()

I have been following this tutorial on using GraphQL and it has told me to write this block of code in my src/index.js file:
const express = require('express');
const bodyParser = require('body-parser');
const {graphqlExpress, graphiqlExpress} = require('apollo-server-express');
const schema = require('./schema');
// 1
const connectMongo = require('./mongo-connector');
// 2
const start = async () => {
// 3
const mongo = await connectMongo();
var app = express();
app.use('/graphql', bodyParser.json(), graphqlExpress({
context: {mongo}, // 4
schema
}));
app.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
}));
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Hackernews GraphQL server running on port ${PORT}.`)
});
};
// 5
start();
Though when I try to run the code using: node ./src/index.js it gives me this error:
const start = async () => {
^
SyntaxError: Unexpected token (
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.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
I've searched online and it seems that if might be caused by the node.js version from here but I've checked my noed.js version and it's 8.3.0 so it should be supported without having to use Babel if I'm not mistaken.
Here is my package.json file:
{
"name": "graphql-js-tutorial",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"apollo-server-express": "^1.1.2",
"body-parser": "^1.17.2",
"express": "^4.15.4",
"graphql": "^0.11.2",
"graphql-tools": "^1.2.2",
"mongodb": "^2.2.31"
}
}
async functions are only available since node 8.3
your code is equivalent to (without async/await)
const start = () => {
return connectMongo().then(mongo => {
var app = express();
app.use('/graphql', bodyParser.json(), graphqlExpress({
context: {mongo}, // 4
schema
}));
app.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
}));
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Hackernews GraphQL server running on port ${PORT}.`)
});
return;
});
};
start your app by the command node yourAppFile -harmony!
async function is available in the harmony mode under Node7.+

Cannot read property 'sync' of undefined

I am following a tutorial on how to set up a basic mvc in Nodejs using the hapi server and a few other packages.
Tutorial: https://www.sitepoint.com/node-js-mvc-application/
Git for my project: https://github.com/christoph88/node-js-mvc-tut/
I have an error when I try to launch the server:
~/Projects/node-js-mvc-tut$ node server.js
/home/christoph/Projects/node-js-mvc-tut/server.js:33
Models.sequelize.sync().then(() => {
^
TypeError: Cannot read property 'sync' of undefined
at Object.<anonymous> (/home/christoph/Projects/node-js-mvc-tut/server.js:33:17)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3
Model is defined within the requirements. I do not understand why sync is undefined. Sequelise is required within the lib/index.js file.
'use strict';
const Hapi = require('hapi');
const Hoek = require('hoek');
const Path = require('path');
const Settings = require('./settings');
const Routes = require('./lib/routes');
const Models = require('./lib/models/');
const server = new Hapi.Server();
server.connection({ port: Settings.port });
server.register([
require('vision'),
require('inert')
], (err) => {
Hoek.assert(!err, err);
server.views({
engines: { pug: require('pug') },
path: Path.join(__dirname, 'lib/views'),
compileOptions: {
pretty: false
},
isCached: Settings.env === 'production'
});
// Add routes
server.route(Routes);
});
Models.sequelize.sync().then(() => {
server.start((err) => {
Hoek.assert(!err, err);
console.log(`Server running at: ${server.info.uri}`);
});
});
Can somebody help me with this? Would be great to get this running so I can try to adapt it to my needs.
I have got it working by moving my index.js file to the models folder.
This file has the necessairy scripts that dispatches sequelise in the model thus fixing the problem.
Make sure you have exported the db in index.js
module.exports = db
and declared a db variable at the beginning of the file
var db = {}

Categories

Resources