错误:找不到模块请验证 package.json 是否具有有效的“main”条目

问题描述

当我创建实施 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"
  }
}

User.js

const mongoose = require('mongoose');
const bcrypt = require('bcrpyt');

const UserSchema = new mongoose.Schema({
  firstName: {
    type: String,default: ''
  },lastName: {
    type: String,email: {
    type: String,password: {
    type: String,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,next) {
    //   const counter = new Counter();
  
    //   counter.save()
    //     .then(() => res.json(counter))
    //     .catch((err) => next(err));
    // });

    /*
    * Sign up
    */
   app.post('/api/account/signup',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'
                });
            });
        });

   });
};

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)