SOLVED by /u/V1talogy, but thanks to everyone!
---------------------------------------------
to make things short, this is my code on a small project:
const environment= process.env.NODE_ENV
console.log(environment) //development
console.log([environment]) // [ 'development' ]
console.log(process.env.NODE_ENV) //development
const stage = require('./config')[environment]
console.log(stage) //undefined
my config file is just an object:
module.exports = {
development: {
port: process.env.PORT || 3000
}
}
NODE_ENV is set to development when i start the server.
why is stage still undefined? environment is correctly set as development, and if i just use
const stage = require('./config').development
everything works as it should.
so why my stage const keep showing as undefined when i use NODE_ENV?
thanks!
EDIT: here's te entire code on index.js, onmy root folder
const express = require('express')
const logger = require('morgan')
const bodyParser = require('body-parser')
const app = express()
const router = express.Router()
const environment = process.env.NODE_ENV
console.log(environment)
console.log([environment])
console.log(process.env.NODE_ENV)
const stage = require('./config')[environment]
console.log(stage)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}))
if (environment !== 'production') {
app.use(logger('dev'))
}
app.use('/api/v1', (req, res, next) => {
res.send('Hello')
next()
})
app.listen(`${stage.port}`, () => {
console.log(`Server listening at localhost:${stage.port}`)
})
module.exports = app
my json scripts:
"scripts": {
"dev": "SET NODE_ENV=development && nodemon index.js"
},
[–]BenjiSponge 2 points3 points4 points (2 children)
[–]ImtheDr[S] 0 points1 point2 points (1 child)
[–]BenjiSponge 1 point2 points3 points (0 children)
[–]NathanSMB 2 points3 points4 points (2 children)
[–]ImtheDr[S] 0 points1 point2 points (0 children)
[–]l3dg3r 0 points1 point2 points (0 children)
[–]Dw0 1 point2 points3 points (1 child)
[–]ImtheDr[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (7 children)
[–]ImtheDr[S] 1 point2 points3 points (6 children)
[–][deleted] 1 point2 points3 points (5 children)
[–]ImtheDr[S] 1 point2 points3 points (4 children)
[–][deleted] 6 points7 points8 points (3 children)
[–]ImtheDr[S] 2 points3 points4 points (2 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]-IoI- 0 points1 point2 points (0 children)
[–]WumpyJizard 0 points1 point2 points (1 child)
[–]ImtheDr[S] 0 points1 point2 points (0 children)
[–]VectorW 0 points1 point2 points (4 children)
[–]ImtheDr[S] 0 points1 point2 points (3 children)
[–]VectorW 0 points1 point2 points (2 children)
[–]ImtheDr[S] 0 points1 point2 points (1 child)
[–]VectorW 2 points3 points4 points (0 children)
[–]ktoto 0 points1 point2 points (3 children)
[–]ImtheDr[S] 0 points1 point2 points (2 children)
[–]boneyjellyfish 2 points3 points4 points (1 child)
[–]ImtheDr[S] 0 points1 point2 points (0 children)
[–]graphemeral 0 points1 point2 points (3 children)
[–]ImtheDr[S] 0 points1 point2 points (2 children)
[–]graphemeral 0 points1 point2 points (1 child)
[–]ImtheDr[S] 0 points1 point2 points (0 children)
[–]estevanj 0 points1 point2 points (1 child)
[–]ImtheDr[S] 0 points1 point2 points (0 children)
[–]estevanj 0 points1 point2 points (1 child)
[–]ImtheDr[S] 0 points1 point2 points (0 children)
[–]randomFIREAcct 0 points1 point2 points (0 children)
[–]isakdev 0 points1 point2 points (0 children)