Rumah  >  Soal Jawab  >  teks badan

Ralat "MODULE NOT FUND", modul tidak ditemui

Saya cuba menyelesaikan masalah laluan:

Ini adalah struktur fail saya: /www/html/

  1. databaseConnection.js
  1. LoginPageScript.js

Dalam fail js pangkalan data, kod berikut:

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

Saya menganggap eksport modul pool

Sekarang saya cuba mengimport kumpulan ke dalam skrip lain, dalam kes ini LoginPageScript.js, menggunakan:

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

Tetapi ralat yang dilemparkan ialah:

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'
  ]
}

Saya cuba mencuba jalan yang berbeza seperti:

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

Tiada satu pun daripada mereka nampaknya berfungsi

P粉201448898P粉201448898217 hari yang lalu535

membalas semua(1)saya akan balas

  • P粉302484366

    P粉3024843662024-04-03 13:35:23

    Cuba melintasi databaseConnection.js fail melalui ulasan titik.

    Dan cuba gunakan:

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

    balas
    0
  • Batalbalas