server - 错误 : Cannot find module. 请验证 package.json 是否具有有效的 "main"条目

标签 server module backend mern

当我创建实现 bcyrpt 的 MERN 堆栈登录系统时,我已在根文件夹中执行“npm install”和“npm install bcrpyt --save”以使服务器正常工作。但是,由于未知的原因,我不断收到错误消息“错误:找不到模块。请验证 package.json 是否具有有效的“主”条目。”

这是我运行 npm run start:dev 时在终端中看到的错误消息

enter image description here

我在 User.js 文件中实现了日志系统,并在顶部编写了代码 const bcrypt = require('bcrpyt'); (不确定这是否会影响它)。

我尝试删除node_module文件并通过键入“npm install”重新安装它,但它仍然不起作用。我不确定在哪里解决这个问题。

这是我当前在文件夹中的结构:

enter image description here

包-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"
  }
}

用户.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);

登录.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'
                });
            });
        });

   });
};

最佳答案

它是“bcrypt”,而不是“bcrpyt”。

关于server - 错误 : Cannot find module. 请验证 package.json 是否具有有效的 "main"条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68097937/

相关文章:

powershell - 如果使用率高于%PowerShell,则向用户发送电子邮件

Python myro 模块导入错误

Python 包导入冲突(Theano 和 openturns)

python - 如何在 imsave() (Agg 后端)中设置 PNG 的压缩参数?

linux - 与 Virtualbox 通信的 Windows 程序

Mysql远程连接

android - 从服务器到应用程序的双向直接通信,无需推送通知

node.js - Nodejs : how to redirect subdomains to ports?

javascript - 在我的简单 Node Js 服务器中解析请求

scala - 玩! 2.5 : Is there a way to bind actors only in Prod mode?