我正在嘗試使用 dotenv-webpack 訪問我的 .env 檔案變數,以便以后可以將它們捆綁在一起,并且我可以在我的網站上使用它們,但是我按照說明進行了操作,并且在網上查看并無法使其正常作業。
//.env
VAR=1234
// created a webpack.config.js with the following code
const Dotenv = require('dotenv-webpack');
module.exports = {
plugins: [
new Dotenv()
]
};
// index.js
console.log(process.env.VAR);
// undefined
我不確定該怎么做,因為我已經按照以下示例進行操作:https ://github.com/mrsteele/dotenv-webpack看起來應該很容易......
uj5u.com熱心網友回復:
為什么不使用dotenv包?
dotenv
uj5u.com熱心網友回復:
正如 Pandhu Wibowo 所建議的,請給他豎起大拇指,我設法使用 webpack-cli、webpack 和 dotenv 包使其作業。 如何將 .env 檔案變數傳遞給 webpack 配置?
./env
VAR=1234
./webpack.config.js
const webpack = require('webpack');
// replace accordingly './.env' with the path of your .env file
require('dotenv').config({ path: './.env' });
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
plugins: [
new webpack.DefinePlugin({
"process.env": JSON.stringify(process.env)
}),
]
};
./package.json
"scripts": {
"start": "webpack"
},
npm run start
--> creates the bundle.js which will include the env variable
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/411423.html
標籤:
