'Error: Cannot find module. Please verify that the package.json has a valid "main" entry
As I'm creating MERN stack login-system with bcyrpt implemented, I have done 'npm install' and 'npm install bcrpyt --save' in my root folder for server to work. However, for unknown reason, I'm constantly getting an error stating "Error: Cannot find module. Please verify that the package.json has a valid "main" entry."
This is the error message I see in the terminal when I run npm run start:dev
I have implemented loging system in the User.js file and wrote a code const bcrypt = require('bcrpyt');
at the top (not sure whether this is affecting it).
I have tried deleting node_module file and reinstalling it by typing 'npm install', but it still does not work. I'm not sure where the go-about fixing this issue.
This is the structure I currently have in the folder:
PACKAGE-JSON
{
"name": "MERN-boilerplate",
"version": "1.0.0",
"description": "MERN stack project boilerplate",
"author": "Eugene Cheung",
"repository": {
"type": "git",
"url": "git+https://github.com/arkon/MERN-boilerplate.git"
},
"license": "MIT",
"private": true,
"scripts": {
"start": "webpack -p --progress --profile --colors && node server",
"start:dev": "node server"
},
"engines": {
"node": ">=6"
},
"dependencies": {
"@babel/core": "^7.0.0-beta.42",
"@babel/preset-env": "^7.0.0-beta.42",
"@babel/preset-react": "^7.0.0-beta.42",
"autoprefixer": "^8.2.0",
"babel-loader": "^8.0.0-beta.2",
"bcrpyt": "^2.0.0",
"connect-history-api-fallback": "^1.5.0",
"copy-webpack-plugin": "^4.5.1",
"css-loader": "^0.28.11",
"express": "^4.16.3",
"extract-text-webpack-plugin": "^4.0.0-beta.0",
"html-webpack-plugin": "^3.1.0",
"mongoose": "^5.0.11",
"node-sass": "^4.7.2",
"nodemon": "^1.17.2",
"postcss-loader": "^2.1.3",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-hot-loader": "^4.0.0",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"sass-loader": "^6.0.7",
"style-loader": "^0.20.3",
"webpack": "^4.2.0",
"webpack-cli": "^2.0.13",
"webpack-dev-middleware": "^3.0.1",
"webpack-hot-middleware": "^2.21.2",
"webpack-merge": "^4.1.2",
"whatwg-fetch": "^2.0.3"
}
}
User.js
const mongoose = require('mongoose');
const bcrypt = require('bcrpyt');
const UserSchema = new mongoose.Schema({
firstName: {
type: String,
default: ''
},
lastName: {
type: String,
default: ''
},
email: {
type: String,
default: ''
},
password: {
type: String,
default: ''
},
isDelete: {
type: Boolean,
default: false
}
});
UserSchema.methods.generateHash = function(password) {
return bcrypt.hashSync(password, bcyrpt.genSaltSync(8), null);
};
UserSchema.methods.validPassword = function(password) {
return bcrpyt.compareSync(password, this.password);
};
module.exports = mongoose.model('User', UserSchema);
signin.js
const User = require('../../models/User')
module.exports = (app) => {
// app.get('/api/counters', (req, res, next) => {
// Counter.find()
// .exec()
// .then((counter) => res.json(counter))
// .catch((err) => next(err));
// });
// app.post('/api/counters', function (req, res, next) {
// const counter = new Counter();
// counter.save()
// .then(() => res.json(counter))
// .catch((err) => next(err));
// });
/*
* Sign up
*/
app.post('/api/account/signup', (req, res, next) => {
const { body } = req;
const {
firstName,
lastName,
password
} = body;
let {
email
} = body;
if (!firstName) {
return res.send({
success: false,
message: 'Error: First name cannot be blank.'
});
}
if (!lastName) {
return res.send({
success: false,
message: 'Error: Last name cannot be blank.'
});
}
if (!email) {
return res.send({
success: false,
message: 'Error: Email cannot be blank.'
});
}
if (!password) {
return res.send({
success: false,
message: 'Error: Password cannot be blank.'
});
}
console.log('here');
email = email.toLowerCase();
// Steps:
// 1. Verify email doesn't exist
// 2. Save
User.find({
email: email
}, (err, previousUsers) => {
if (err) {
return res.send({
success: false,
message: 'Error: Server error'
});
} else if (previousUsers.length > 0) {
return res.send({
success: false,
message: 'Error: Account already exist.'
});
}
// Save the new user
const newUser = new User();
newUser.email = email;
newUser.firstName = firstName;
newUser.lastName = lastName;
newUser.password = newUser.generateHash(password);
newUser.save((err, user) => {
if (err) {
return res.send({
success: false,
message: 'Error: Password cannot be blank.'
});
}
return res.send({
success: true,
message: 'Signed up'
});
});
});
});
};
Solution 1:[1]
It's "bcrypt", not "bcrpyt".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ChromiumCat |