错误'MODULE NOT FOUND”,找不到模块
P粉201448898
P粉201448898 2024-04-02 17:27:55
0
1
551

我正在尝试解决路径问题:

这就是我的文件结构: /www/html/

  • /数据库/
  1. databaseConnection.js
  • /脚本/
  1. LoginPageScript.js
  • 服务器.js
  • index.html

在数据库js文件中,如下代码:

const mysql = require('mysql2');

const pool = mysql.createPool({
  connectionLimit: 10,
  host: process.env.DB_HOST,
  port: process.env.DB_PORT,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DATABASE
});


pool.getConnection((err, connection) => {
  if (err) {
    console.error('Error connetcting to database: ', err);
    return;
  }
  console.log('Connection to database succefully established!');
});

module.exports = { pool };

我假设导出池模块

现在我正在尝试将池导入到另一个脚本中,在本例中为 LoginPageScript.js,使用:

const { pool } = require('../database/databaseConnection.js');

但是抛出的错误是:

Cannot find module '../database/databaseConnection'
Require stack:
- /var/www/html/scripts/LoginPageScript.js
- /var/www/html/server.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
    at Module._load (node:internal/modules/cjs/loader:804:27)
    at Module.require (node:internal/modules/cjs/loader:1022:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/var/www/html/scripts/LoginPageScript.js:3:18)
    at Module._compile (node:internal/modules/cjs/loader:1120:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1174:10)
    at Module.load (node:internal/modules/cjs/loader:998:32)
    at Module._load (node:internal/modules/cjs/loader:839:12)
    at Module.require (node:internal/modules/cjs/loader:1022:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/www/html/scripts/LoginPageScript.js',
    '/var/www/html/server.js'
  ]
}

我尝试尝试不同的路径,例如:

  1. /database/databaseConnection.js
  2. ./database/databaseConnection.js
  3. ../database/databaseConnection.js

似乎没有一个起作用

P粉201448898
P粉201448898

全部回复(1)
P粉302484366

尝试通过点注释遍历 databaseConnection.js 文件。

并尝试使用:

const { pool } = require('./database/databaseConnection.js');
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板