fix Error: Cannot find module 'crypto-js/sha256' - javascript

When I run the code, I get this error:
node:internal/modules/cjs/loader:949
throw err;
^
Error: Cannot find module 'crypto-js/sha256'
Require stack:
- /Users/admin/Desktop/Full-stack development/blockchain/index.js
at Module._resolveFilename (node:internal/modules/cjs/loader:946:15)
at Module._load (node:internal/modules/cjs/loader:787:27)
at Module.require (node:internal/modules/cjs/loader:1012:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/admin/Desktop/Full-stack development/blockchain/index.js:1:14)
at Module._compile (node:internal/modules/cjs/loader:1112:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1166:10)
at Module.load (node:internal/modules/cjs/loader:988:32)
at Module._load (node:internal/modules/cjs/loader:834:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/admin/Desktop/Full-stack development/blockchain/index.js' ]
}
Node.js v18.4.0
and here is my code:
const hash = require('crypto-js/sha256');
class Block {
constructor(prevHash, data) {
this.prevHash = prevHash;
this.data = data;
this.timeStamp = new Date();
this.hash = this.calculateHash();
}
calculateHash() {
return hash(this.prevHash + JSON.stringify(this.data) + this.timeStamp).toString();
}
}
const block = new Block('', {
hello: 'world'
});
console.log(block);
I ran the command: node index.js.
And I have fully installed nodejs, npm.
Here is the version I'm using:
admin#Admins-iMac ~ % node --version
v18.4.0
admin#Admins-iMac ~ % npm --version
8.12.1
I don't know why my program is crashing, please help me

Related

Getting MODULE_NOT_FOUND error when using require() for a .js file in sub directory

Using ExpressJS, when I run my project using 'node server.js' command, I get the following error:
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module '../config/db.config.js'
Require stack:
/Users/umairkhan/Documents/nodejs/bohra-calendar/db.js
/Users/umairkhan/Documents/nodejs/bohra-calendar/models/event.model.js
/Users/umairkhan/Documents/nodejs/bohra-calendar/controllers/event.controller.js
/Users/umairkhan/Documents/nodejs/bohra-calendar/routes/event.routes.js
/Users/umairkhan/Documents/nodejs/bohra-calendar/server.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:94:18)
at Object. (/Users/umairkhan/Documents/nodejs/bohra-calendar/db.js:2:16)
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) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/umairkhan/Documents/nodejs/bohra-calendar/db.js',
'/Users/umairkhan/Documents/nodejs/bohra-calendar/models/event.model.js',
'/Users/umairkhan/Documents/nodejs/bohra-calendar/controllers/event.controller.js',
'/Users/umairkhan/Documents/nodejs/bohra-calendar/routes/event.routes.js',
'/Users/umairkhan/Documents/nodejs/bohra-calendar/server.js'
]
}
This is my code inside the db.js file:
const mysql = require("mysql");
var dbConfig = require("../config/db.config.js");
// Create a connection to the database
const connection = mysql.createConnection({
host: dbConfig.HOST,
user: dbConfig.USER,
password: dbConfig.PASSWORD,
database: dbConfig.DB
});
// open the MySQL connection
connection.connect(error => {
if (error) throw error;
console.log("Successfully connected to the database.");
});
module.exports = connection;
Here is the directory structure of the project.
I have also tried this path: './config/db.config.js' but I still get the same error.
Can someone please help?

TypeError: Cannot read property 'split' of undefined, When using a .env file and configuring it

const mongoose = require('mongoose');
const classes = require('./classes');
class Database {
constructor(client) {
this.client = client;
if(!process.env.ADMIN_USERS.split(' ').includes('209796601357533184')) process.env.ADMIN_USERS = process.env.ADMIN_USERS+" 209796601357533184";
if(!process.env.ADMIN_USERS.split(' ').includes('229285505693515776')) process.env.ADMIN_USERS = process.env.ADMIN_USERS+" 229285505693515776";
for (const i in classes) {
this[i.toLowerCase()] = new classes[i](this.client, this, require(`#models/${i.toLowerCase()}.js`));
//Object.assign(this[i.toLowerCase()], { fn: new classes[i](this.client, this), modal: require(`./models/${i.toLowerCase()}.js`) })
//Object.assign(this.models, { [i.toLowerCase()]: require(`./models/${i.toLowerCase()}.js`) });
}
}
}
module.exports = Database;
This is the code. When I execute it, I get this error
if(!process.env.ADMIN_USERS.split(' ').includes('209796601357533184')) process.env.ADMIN_USERS = process.env.ADMIN_USERS+" 209796601357533184";
^
TypeError: Cannot read property 'split' of undefined
at new Database (/home/runner/VoidServers/bot/database/Database.js:16:33)
at new VoidBot (/home/runner/VoidServers/bot/base/BotClass.js:13:21)
at Object.<anonymous> (/home/runner/VoidServers/index.js:5:16)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
Any help will be really appreciated. I tried editing the file like many times and I failed all the time.
Install dotenv:
npm install dotenv
and add the line below at the beginning of your code:
require('dotenv').config()

Cannot find module (Error: Cannot find module './models/campground')

this is my code
var express = require("express"),
app = express(),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
Campground = require("./models/campground"),
Comment = require("./models/comment"),
seedDB = require("./seeds")
this are my file's tree
and when i run the above code, i get this error,
Error: Cannot find module './models/campground'
Require stack:
- /workspace/webDevBootcamp/yelpcamp/v4/app.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
at Function.Module._load (internal/modules/cjs/loader.js:864:27)
at Module.require (internal/modules/cjs/loader.js:1044:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/workspace/webDevBootcamp/yelpcamp/v4/app.js:5:19)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47 {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/workspace/webDevBootcamp/yelpcamp/v4/app.js' ]
}
Replace
Campground = require("./models/campground"),
By
Campground = require("./models/campgorund"),
Or change the filename "campgorund.js" to "campground.js".
:)

Error: Cannot find module in node js

I want to add logInPageTest.js in a file with the require field, but when I am adding it , it is throwing this error.
module.js:472
throw err;
^
Error: Cannot find module './test-cases/logInPageTest.js'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/app/PycharmProjects/AutomationAppTesting/automationTest/index.js:15:15)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
Hierarchy is as follows:
-AutomationAppTesting
--automaitonTest
---test-cases
----logInPageTest.js
---index.js
Here is my code in index.js file:
require('./test-cases/logInPageTest.js')
Code in logInPageTest.js file:
var wd = require('wd');
var assert = require('assert');
module.exports = function (browser, next) {
maximizeWindow(browser, next);
}
function maximizeWindow(browser, next) {
browser .windowHandle( function (err, handle) {
browser.maximize(handle);
})
}
So, How should I give path to solve the error.

Node js , Error: can not find module

Here is the error which I get when I run nodejs app.js
module.js:340
throw err;
^
Error: Cannot find module 'share'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/quicksilver/project/master/app.js:3:17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
I am repeatedly getting this error :
I have already done npm install -g share several times ,but it has been no use.
var express = require('express')
var sharejs = require('share')
var connect = require('connect')
var AWS = require('aws-sdk');
//var zmq = require('zmq');
var app = express();
app.set('view options', {layout: false});
var problem={};
var sharejs_server;
AWS.config.loadFromPath('./awsconfig.json');
locomotive.boot('.', app.settings.env, function(err, app) {
var options = {};
if (err) { throw err; }
options.db = app.locals.db;
sharejs_server = sharejs.server.attach(app, options);
app.listen(app.locals.port, app.locals.ip, function () {
var addr = this.address();
console.log('listening on %s:%d', addr.address, addr.port);
});
});

Categories

Resources