Reputation: 5106
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:
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:
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!")
Upvotes: 0
Views: 1364
Reputation: 1182
if you just write it config
then it will search on node_modules
or nodejs api. you should specifically write it ./config
then it will work.
./
indicates local module on this folder. and use ../
to load module from parent directory.
Upvotes: 2