How to extract environment variable from process.environment in nodejs

拈花ヽ惹草 提交于 2021-01-28 18:37:31

问题


I have successfully integrated stripe payments in my dummy nodejs project. But still I'm left with an issue which doesn't let me move forward as I have to push code to my github repository. So, I installed dotenv package through npm and created a .env file for storing my stripe secret key. If I extract stripe secret key using process.env.STRIPE_SECRET_KEY payment fails but If I pass STRIPE_SECRET_KEY directly payment succeeds.

I have even consoled process.env and it does show my stripe secret key.

This is my server.js file:

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

const payments = require('./routes/api/payment');

dotenv.config();

const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use(cors())

app.use('/api/payments', payments);

app.get('/', (req, res) => res.send('hello'));

const port = process.env.PORT || 3001;

app.listen(port, () => console.log(`server running on port: ${port}`));

This is my payment.js file:

const express = require('express');
const stripe = require('stripe') (process.env.STRIPE_SECRET_KEY); // this 
                                                       gives network error
const stripe = require('stripe') ("sk_test_O5le6FijZU4KcoxE4"); // this 
                                                           works gracefully
const router = express.Router();

router.get('/', (req, res) => {
  res.json({
   message: 'Payment gateway works',
   time: Date.now
  });
})

router.post('/', (req, res) => {
  const credentials = {
  source: req.body.token.id,
  amount: req.body.amount,
  currency: "usd",
  description: req.body.description
}

stripe.charges.create(credentials, (err, response) => {
  if (err) {
  return res.status(400).json({ message: err })
   }
  return res.status(200).json({ message: response })
  })

  })

module.exports = router;

This is my .env file:

STRIPE_SECRET_KEY = sk_test_O5le6FijZU4KcoxE4

directly passing stripe secret key to the stripe works but it fails if I provide stripe secret key using process.env.STRIPE_SECRET_KEY. I want process.env.STRIPE_SECRET_KEY to work so that I can push code to my github repository safely and securely.


回答1:


I see the problem here You need to initialize dontenv after main imports!

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

dotenv.config(); // here after main imports

// After dotenv.config() payment.js will have dontenv values!!
const payments = require('./routes/api/payment');




回答2:


You have to change in server.js file for dotenv npm. Like **require('dotenv').config();** then after may be you have export process env.

module.exports = {
  STRIPE_SECRET_KEY: process.env.STRIPE_SECRET_KEY,
};


来源:https://stackoverflow.com/questions/57882573/how-to-extract-environment-variable-from-process-environment-in-nodejs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!