[英]Allowing access to process and process.env in Webpack 5
I'm using environment variables to set the basename of a react-router-dom BrowserRouter.我正在使用环境变量来设置 react-router-dom BrowserRouter 的基本名称。 However, Webpack 5 does not allow accessing environment variables.
但是,Webpack 5 不允许访问环境变量。
From other stackoverflow questions, I've seen that using this:从其他 stackoverflow 问题中,我已经看到使用这个:
new webpack.ProvidePlugin({
process: 'process/browser',
})
makes process
available.使
process
可用。 However, process.env
is empty ( {}
).但是,
process.env
是空的 ( {}
)。
Also, if I use另外,如果我使用
const dotenv = require('dotenv')
dotenv.config();
console.log(process.env);
I can see the variables in my.env file are present in process.env
in the config file but I can't find a way to make them accessible from process.env
in the code.我可以看到 my.env 文件中的变量存在于配置文件的
process.env
中,但我找不到一种方法使它们可以从代码中的process.env
访问。
I have tried:我努力了:
new Dotenv({ systemvars: true })
And some other plugins suggested in Webpack: Bundle.js - Uncaught ReferenceError: process is not defined but adding any other plugin other than the 'process/browser'
makes process not defined again. Webpack 中建议的其他一些插件:Bundle.js - Uncaught ReferenceError: process is not defined但添加除
'process/browser'
以外的任何其他插件会使 process 不再定义。
I know there's a reason behind not allowing the use of process.env
but I do need it.我知道不允许使用
process.env
是有原因的,但我确实需要它。
DotenvPlugin Dotenv插件
The third-party DotenvPlugin (dotenv-webpack) allows you to expose (a subset of) dotenv variables:第三方 DotenvPlugin (dotenv-webpack) 允许您公开(一部分)dotenv 变量:
new Dotenv({
path: './.env', // Path to .env file (this is the default)
safe: true, // load .env.example (defaults to "false" which does not use dotenv-safe)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.