【问题标题】:Express Not Installing快递不安装
【发布时间】:2021-11-20 22:54:59
【问题描述】:

我知道以前有人问过这个问题,但没有一个解决方案对我有用。每当我尝试安装 express 时,它似乎工作得很好,显示以下消息:

npm WARN website@1.0.0 No description

+快递@4.17.1 在 6.581 秒内更新了 1 个包并审核了 50 个包 发现 0 个漏洞

但是,当我尝试运行使用 express 的程序时:

 at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15)
    at Module.require (internal/modules/cjs/loader.js:961:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object.<anonymous> (C:\Users\darcy\OneDrive\Sutton\Website\server.js:2:15)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) {
  code: 'MODULE_NOT_FOUND',`

这是我的代码:

let express = require("express");
let account = require("accountBack");
let shop = require("shopBack")

let serverConnection = express();
serverConnection.listen(3000, () => {});
serverConnection.use(express.static("public"));
serverConnection.use(express.json());
serverConnection.post("/account", account.accountServer);
serverConnection.get("/shop", shop.sendShop);

我正在使用其他文件中的代码(因此 额外的 2 个要求声明)但我认为它们的作用并不重要。

以下是我输入的命令:

npm init
npm install express
node server.js

从错误中看不出 express 安装是否正确,但我不确定。是我输入的命令还是其他问题?任何帮助将不胜感激。

【问题讨论】:

标签: javascript node.js express npm


【解决方案1】:

对于内部模块,你应该像这样导入

const account = require("./accountBack");
const shop = require("./shopBack")

除非您将这些模块发布到 npm 注册表(私有/公共),否则您可以像以前一样导入。

【讨论】:

  • 这个。根据nodejs.org/en/knowledge/getting-started/what-is-requireThe rules of where require finds the files can be a little complex, but a simple rule of thumb is that if the file doesn't start with "./" or "/", then it is either considered a core module (and the local Node.js path is checked), or a dependency in the local node_modules folder. If the file starts with "./" it is considered a relative file to the file that called require. If the file starts with "/", it is considered an absolute path. NOTE: you can omit ".js" and require will automatically append it if needed.
  • 有效,但现在返回不同的错误。这是一个简单的,所以我应该能够弄清楚。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2014-12-05
  • 2013-08-12
  • 1970-01-01
  • 2016-07-02
  • 2017-07-26
  • 2014-07-22
  • 2011-07-15
  • 1970-01-01
相关资源
最近更新 更多