【问题标题】:In Mongoose, can't find module '\node_modules\ipaddr.js\lib\ipaddr.js'. Please verify that the package.json has a valid "main" entry在 Mongoose 中,找不到模块 '\node_modules\ipaddr.js\lib\ipaddr.js'。请验证 package.json 是否有有效的“main”条目
【发布时间】:2021-06-14 07:23:42
【问题描述】:

我正在做一个待办事项清单上的项目。这是我的 app.js 文件代码。

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");
});

我已经正确安装了包括猫鼬在内的所有软件包。之后,我在 2 个不同的命令提示符下启动了 mongo 服务器,编写了这段代码。 mongod mongo 之后我通过写node app.js 来启动服务器。 我收到了这个错误:

      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'

请告诉我我现在该怎么办?

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    我从其他问题得到了答案: 只需运行以下命令

    rm -rf node_modules

    npm i

    通过运行这些命令,我​​已经成功启动了我的服务器。?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-21
      • 1970-01-01
      • 2021-05-18
      • 1970-01-01
      • 2022-07-14
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多