No such file directory when compile solidity with node fs library - javascript

I'm trying to compile solidity using node compile.js. I have attached the code below:
Here is my simple demo structure:
contracts
node_modules
compile.js
Incrementer.sol
package.json
package-lock.json
Here is compile.js
const fs = require('fs');
const solc = require('solc');
const path = require('path');
// Get Path and Load Contract
const inboxPath = path.resolve(__dirname, 'Incrementer.sol');
console.log(inboxPath) // This one provide directory to read (/Users/amstriker/Desktop/Sdax/OnePlace/contracts/Incrementer.sol'
const source = fs.readFileSync(inboxPath, 'utf8'); // still getting stuck on this
// Compile Contract
const input = {
language: 'Solidity',
sources: {
'Incrementer.sol': {
content: source,
},
},
settings: {
outputSelection: {
'*': {
'*': ['*'],
},
},
},
};
const tempFile = JSON.parse(solc.compile(JSON.stringify(input)));
const contractFile = tempFile.contracts['Incrementer.sol']['Incrementer'];
// Export Contract Data
module.exports = contractFile;
Incrementer.sol:
pragma solidity ^0.8.0;
contract Incrementer {
uint256 public number;
constructor(uint256 _initialNumber) {
number = _initialNumber;
}
function increment(uint256 _value) public {
number = number + _value;
}
function reset() public {
number = 0;
}
}
Packages.json
{
"dependencies": {
"solc": "^0.8.0",
"web3": "^1.5.3"
}
}
I have been tried many different approach but still getting errors:
internal/fs/utils.js:314
throw err;
^
Error: ENOENT: no such file or directory, open
'/Users/amstriker/Desktop/Sdax/OnePlace/contracts/Incrementer.sol'
at Object.openSync (fs.js:498:3)
at Object.readFileSync (fs.js:394:35)
at Object.<anonymous> (/Users/amstriker/Desktop/Sdax/OnePlace/contracts/compile.js:7:19)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Module.require (internal/modules/cjs/loader.js:961:19)
at require (internal/modules/cjs/helpers.js:92:18)
at Object.<anonymous> (/Users/steven/Desktop/Sdax/OnePlace/contracts/get.js:2:17)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47 {
errno: -2,
syscall: 'open',
code: 'ENOENT',
path: '/Users/amstriker/Desktop/Sdax/OnePlace/contracts/Incrementer.sol'
}
Any helps would be appreciated!!.

It failed because of space solidity filename( Incrementer.sol). It is working fine after i changed from Incrementer.sol to Incrementer.sol.

Related

Error: Cannot find module 'renderer/utils/localStore'

I am trying to call userDir function to get userdata folder path in the main/main.ts. I am using the electron-react-boilerplate
const electron = require('electron');
const path = require('path');
const fs = require('fs');
export function userDir() {
return (electron.app || electron.remote.app).getPath('userData');
}
I am caling the userDir function in the main.ts file
import { userDir } from 'renderer/utils/localStore';
userDir()
Error:
Electron encountered an error
Error: Cannot find module 'renderer/utils/localStore'
Require stack:
- /home/prash/Projects/Electron/pomodoro/src/main/main.ts
- /home/prash/Projects/Electron/pomodoro/node_modules/.pnpm/electron#18.3.2/node_modules/electron/dist/resources/default_app.asar/main.js
-
at Module._resolveFilename (node:internal/modules/cjs/loader:940:15)
at Function.n._resolveFilename (node:electron/js2c/browser_init:245:1105)
at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/home/prash/Projects/Electron/pomodoro/node_modules/.pnpm/#cspotcode+source-map-support#0.8.1/node_modules/#cspotcode/source-map-support/source-map-support.js:811:30)
at Function.Module._load (/home/prash/Projects/Electron/pomodoro/node_modules/.pnpm/runtime-required#1.1.0/node_modules/runtime-required/runtime-required.js:28:44)
at Module.require (node:internal/modules/cjs/loader:1012:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/home/prash/Projects/Electron/pomodoro/src/main/main.ts:15:1)
at Module._compile (node:internal/modules/cjs/loader:1116:14)
at Module.m._compile (/home/prash/Projects/Electron/pomodoro/node_modules/.pnpm/ts-node#10.8.0_wxuian5givsywcqjoc4lpn66fa/node_modules/ts-node/src/index.ts:1597:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1169:10)
```

Discord.js & Canvacord: Error: libuuid.so.1: cannot open shared object file: No such file or directory

After I added level.js file in my commands folder and ran the bot, it showed up the error:
node:internal/modules/cjs/loader:1183
return process.dlopen(module, path.toNamespacedPath(filename));
^
Error: libuuid.so.1: cannot open shared object file: No such file or directory
at Object.Module._extensions..node (node:internal/modules/cjs/loader:1183:18)
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:94:18)
at Object.<anonymous> (/home/runner/UnfortunateDistortedEngine/node_modules/canvas/lib/bindings.js:3:18)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
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:94:18)
at Object.<anonymous> (/home/runner/UnfortunateDistortedEngine/node_modules/canvas/lib/canvas.js:9:18)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) {
code: 'ERR_DLOPEN_FAILED'
}
The code of level.js file:
const { client, CommandInteraction, MessageEmbed } = require("discord.js");
const db = require('quick.db')
const canvacord = require("canvacord");
const Levels = require('discord-xp')
module.exports = {
name: "level",
description: "Displays the user's level and XP.",
type: 'CHAT_INPUT',
options: [
{
name: "member",
type: "USER",
description: "Specify a user.",
required: false
}
],
run: async (client, interaction, args) => {
let user1 = message.author;
const Blacklisted = db.fetch(`blacklistedUsers_${user1.id}`)
if (Blacklisted === true) return;
let target = await interaction.options.getUser('member')
let mentionedMember = target || interaction.user;
const user = await Levels.fetch(mentionedMember.id, interaction.guild.id, true)
const rank = new canvacord.Rank()
.setAvatar(mentionedMember.displayAvatarURL({ format: 'png', size: 512 }))
.setCurrentXP(mentionedMember.xp || 0)
.setRequiredXP(Levels.xpFor(user.level + 1) || Levels.xpFor(1))
.setRank(user.position)
.setLevel(user.level)
.setStatus(mentionedMember.presence.status)
.setBackground("IMAGE", "https://i.pinimg.com/originals/22/90/0f/22900f16a05211e2562828b6b3fe86b7.jpg")
.setProgressBar("#FFFFFF")
.setUsername(mentionedMember.username)
.setDiscriminator(mentionedMember.discriminator);
rank.build()
.then(data => {
const attachment = new MessageAttachment(data, "RankCard.png");
interaction.followUp({
files: [attachment]
})
});
}
}
There already was an issue opened on GitHub, but the opener didn't state any solution, just said "Solved". This is related to the Canvacord package that uses canvas.
You need libuuid which is a dependency of node-canvas which in return is a dev dependency of canvacord, it is usually preinstalled in many linux systems although you can use
apt-get install libuuid1
to install it, this should solve the issue, please note that you might have to use Nix if you are using a host such as repl.it in which you do not have root access.
Well, if you're using replit it's showing that error... So, I suggest you to change your replit.nix with the given nix below it would definitely fixed your issue!
{ pkgs }: {
deps = [
pkgs.nodejs-16_x
pkgs.libuuid
];
env = {
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [pkgs.libuuid];
};
}

It apparently fails to find the module

The code is this and when I try to turn my bot on I get a error that it can't find the file to execute the command.
const fs = require('fs');
module.exports = (client, Discord) =>{
const commandFolders = fs.readdirSync('./commands')
for(const folder [enter image description here][1]of commandFolders){
const command_files = fs.readdirSync(`./commands/${folder}`).filter(file => file.endsWith('.js'))
for(const file of command_files){
const command = require(`./commands/${folder}/${file}`);
if(command.name){
client.commands.set(command.name, command);
}else {
continue
}
}
}
}
This is the error I get when I turn my bot on.
node:internal/modules/cjs/loader:944
throw err;
^
Error: Cannot find module './commands/Fun/8ball.js'
Require stack:
- C:\Users\Desktop\Discordproject\handlers\command_handler.js
- C:\Users\Desktop\Discordproject\index.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:941:15)
at Function.Module._load (node:internal/modules/cjs/loader:774:27)
at Module.require (node:internal/modules/cjs/loader:1013:19)
at require (node:internal/modules/cjs/helpers:93:18)
at module.exports (C:\Users\Desktop\Discordproject\handlers\command_handler.js:8:25)
at C:\Users\famil\Desktop\Discordproject\index.js:11:37
at Array.forEach (<anonymous>)
at Object.<anonymous> (C:\Users\Desktop\Discordproject\index.js:10:38)
at Module._compile (node:internal/modules/cjs/loader:1109:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\Desktop\\Discordproject\\handlers\\command_handler.js',
'C:\\Users\\Desktop\\Discordproject\\index.js'
]
}
Directory Structure:
Your command_handler.js file is inside /handlers/. ./ is used to access the current directory.
Doing ./commands/ doesn't work because the commands folder is not inside of /handlers/
You must first exit the current folder (using ../), then access.
Change all of the './commands' to '../commands'

Deploying nuxt edge to firebase with function get an error

I try to deploy nuxt project using the latest version of Nuxt which is nuxt-edge to firebase. This is my function code.
const { Nuxt } = require('nuxt-edge')
const express = require('express')
const functions = require('firebase-functions')
const app = express()
const config = {
dev: false,
buildDir: 'nuxt',
build: {
extractCSS: true,
cache: false,
parallel: true,
publicPath: '/assets/'
}
}
const nuxt = new Nuxt(config)
const handleRequest = (req, res) => {
return new Promise((resolve, reject) => {
nuxt.render(req, res, promise => {
promise.then(resolve).catch(reject)
})
})
}
app.use(handleRequest)
exports.jefrydcossr = functions.https.onRequest(app)
It successfully run locally with firebase serve --only hosting,function command. But when I deployed it online and I open the hosting url, I got server error 500.
After I looked at the error log, I got error like this
2018-06-14T12:43:26.094Z D jefrydcossr: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: Error: only one instance of babel-polyfill is allowed
at Object.<anonymous> (/user_code/node_modules/nuxt-edge/node_modules/babel-polyfill/lib/index.js:10:9)
at Module._compile (module.js:577:32)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/user_code/node_modules/nuxt-edge/dist/nuxt-legacy.js:50:1)
at Module._compile (module.js:577:32)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
2018-06-14T12:43:26.206Z E jefrydcossr: undefined
2018-06-14T12:51:04.440Z N jefrydcossr: undefined
2018-06-14T12:53:28.052Z N jefrydcossr: undefined
The repository of this project here https://github.com/jefrydco/jefrydco-id

Call a helper functions anywhere in the applicaion - Node/Express

I have a controller
import request from 'request-promise'
import env from 'dotenv'
const dotenv = env.config();
/*==================================
= getCPEInfo =
==================================*/
function getCPEInfo(req, res)
{
var $cpeMac = req.body.cpeMac;
return new Promise((resolve, reject) => {
request({ uri: `${process.env.API_HOST}/vse/ext/vcpe/${$cpeMac}` })
.then((cpe) => resolve(JSON.parse(cpe).data))
.catch((error) => reject(error));
});
}
module.exports = {
getCPEInfo
};
I want to call this getCPEInfo() on my other files in my project
I tried import
import generalController from './controllers/general.js'
and call it
generalController.getCPEInfo();
I kept getting
[nodemon] restarting due to changes...
[nodemon] starting `babel-node ./index.js`
/Users/bheng/Desktop/express-app/index.js:72
router.post('/getCPEInfo/:cpeMac', generalController.getCPEInfo);
^
ReferenceError: generalController is not defined
at Object.<anonymous> (/Users/bheng/Desktop/express-app/index.js:37:36)
at Module._compile (module.js:643:30)
at loader (/Users/bheng/Desktop/express-app/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/bheng/Desktop/express-app/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at Object.<anonymous> (/Users/bheng/Desktop/express-app/node_modules/babel-cli/lib/_babel-node.js:154:22)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
[nodemon] app crashed - waiting for file changes before starting...
Can someone please give me a hint ?
import generalController from './controllers/general.js' will refer to the default export from the module, you need to refactor your controller :
import request from 'request-promise'
import env from 'dotenv'
const dotenv = env.config();
/*==================================
= getCPEInfo =
==================================*/
function getCPEInfo(req, res)
{
var $cpeMac = req.body.cpeMac;
return new Promise((resolve, reject) => {
request({ uri: `${process.env.API_HOST}/vse/ext/vcpe/${$cpeMac}` })
.then((cpe) => resolve(JSON.parse(cpe).data))
.catch((error) => reject(error));
});
}
var Mycontroller = {
getCPEInfo
};
export default MyController
Take a look here.

Categories