简体   繁体   中英

In Mongoose, can't find module '\node_modules\ipaddr.js\lib\ipaddr.js'. Please verify that the package.json has a valid “main” entry

I'm making a project on To-Do list. Here is my code of app.js file.

const bodyParser = require("body-parser");
const mongoose = require("mongoose");

const app = express();

app.set("view engine", "ejs");

app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));

mongoose.connect("mongodb://localhost:27017/todolistDB", {
  useNewUrlParser: true,
});

const itemsSchema = {
  name: String,
};

const Item = mongoose.model("Item", itemsSchema);

const item1 = new Item({
  name: "Welcome to your TO-Do List!",
});

const item2 = new Item({
  name: "Hit the + button to add new item.",
});

const item3 = new Item({
  name: "<-- Hit this to delete an item.",
});

const defaultItems = [item1, item2, item3];

Item.insertMany(defaultItems, function (err) {
  if (err) {
    console.log(err);
  } else {
    console.log("Successfully inserted items.");
  }
});

app.get("/", function (req, res) {
  res.render("list", { listTitle: "Today", newListItems: items });
});

app.post("/", function (req, res) {
  const item = req.body.newItem;

  if (req.body.list === "Work") {
    workItems.push(item);
    res.redirect("/work");
  } else {
    items.push(item);
    res.redirect("/");
  }
});

app.get("/work", function (req, res) {
  res.render("list", { listTitle: "Work List", newListItems: workItems });
});

app.get("/about", function (req, res) {
  res.render("about");
});

app.listen(3000, function () {
  console.log("Server started on port 3000");
});

I've installed all packages including mongoose correctly. After that I've started mongo server in 2 different command prompt writing this code. mongod mongo After that I've starte server by writing node app.js . and I'm getting this error:

      throw err;
      ^

Error: Cannot find module 'F:\WEB DEVELOPMENT\PROJECTS\todolist-v2-starting-file
s\node_modules\ipaddr.js\lib\ipaddr.js'. Please verify that the package.json has
 a valid "main" entry
    at tryPackage (internal/modules/cjs/loader.js:308:19)
    at Function.Module._findPath (internal/modules/cjs/loader.js:521:18)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:872:27)
    at Function.Module._load (internal/modules/cjs/loader.js:730:27)
    at Module.require (internal/modules/cjs/loader.js:957:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (F:\WEB DEVELOPMENT\PROJECTS\todolist-v2-starting-file
s\node_modules\proxy-addr\index.js:24:14)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32) {
  code: 'MODULE_NOT_FOUND',
  path: 'F:\\WEB DEVELOPMENT\\PROJECTS\\todolist-v2-starting-files\\node_modules
\\ipaddr.js\\package.json',
  requestPath: 'ipaddr.js'

Please let me know what should I do now?

I've got an answer from other question: Just run the following command

rm -rf node_modules

npm i

By running these commands I've started my server successfully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM