简体   繁体   English

错误:找不到模块“配置”:Javascript,模块位于同一文件夹目录中

[英]Error: Cannot find module 'config': Javascript, module is in the same folder directory

I'm trying to import database module in my main express script.我正在尝试在我的主要快递脚本中导入数据库模块。 However I get the但是我得到了

Cannot find module 'config'

error.错误。 I don't understand why, because they are in the same folder and the path I import from is correct.我不明白为什么,因为它们在同一个文件夹中,而且我导入的路径是正确的。

在此处输入图像描述

app.js :应用程序.js

const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const morgan = require('morgan')


const db = require('config')

const app = express()
app.use(morgan('combined'))
app.use(bodyParser.json())
app.use(cors())

app.get('/test', (req, res) => {
  res.json({message: "Testing..."})
})

app.post('/register', async (req, res) =>  {

    console.log("register before db")

    await db.query("INSERT INTO users (username, password, email, created_date) VALUES  ($1, $2, $3);",
        ['test2', '222', 'test2@', '2020-01-02']);

    res.send({
        message: "Registered! Email: " + req.body.email +
            ", password: " + req.body.password
    })
})

app.post("/login", (req, res) => {

})

app.listen(process.env.PORT || 8081)

config.js :配置.js

var pg = require('pg');

const pool = new pg.Pool({
    user: 'myname',
    password: '123456',
    host: 'localhost',
    'port': '5432',
    database: 'mydatabase'
})

pool.on('connect', () =>     {
    console.log('Connected!');
});

module.exports = {
    query: (text, params) => pool.query(text, params),
};

console.log("Done!")

if you just write it config then it will search on node_modules or nodejs api.如果您只是编写config ,那么它将搜索node_modules或 nodejs api。 you should specifically write it ./config then it will work.您应该专门编写它./config然后它将起作用。

./ indicates local module on this folder. ./表示文件夹上的本地模块。 and use ../ to load module from parent directory.并使用../从父目录加载模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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