admin管理员组文章数量:1429998
I develop a Strapi local plugin but I'm unable to retrieve variable defined in my .env file at the root of my project. I try to load this value in my React ponent (plugins/myPluginName/admin/src/containers/HomePage/index.js
).
I try with the global process module like that :
process.env.my_variable
But it returns undefined
Any ideas ?
I develop a Strapi local plugin but I'm unable to retrieve variable defined in my .env file at the root of my project. I try to load this value in my React ponent (plugins/myPluginName/admin/src/containers/HomePage/index.js
).
I try with the global process module like that :
process.env.my_variable
But it returns undefined
Any ideas ?
Share Improve this question edited May 4, 2022 at 8:55 Thibault Walterspieler asked Nov 8, 2020 at 16:49 Thibault WalterspielerThibault Walterspieler 2,4222 gold badges17 silver badges27 bronze badges2 Answers
Reset to default 5Apparently, the admin panel now supports dotenv variables.
Just prefix your .env variable with STRAPI_ADMIN_ and it'll be available using process.env.
For example, STRAPI_ADMIN_KEY
in .env is available as process.env.STRAPI_ADMIN_KEY
Thanks @sunnyson on the strapi forum I found a solution. By default .env variables are not passed to the client-side. You need to customize webpack config.
To do so :
- Create a folder
/admin
at the root of your project then create aadmin.config.js
.
module.exports = {
webpack: (config, webpack) => {
// Add your variable using the DefinePlugin function
config.plugins.push(
new webpack.DefinePlugin({
// ENVS that you want to use in frontend
CUSTOM_VARIABLES: {
variable1: JSON.stringify(process.env.variable1),
},
})
);
// Return the modified config
return config;
},
};
- In your react ponent you can use your env variables like that :
class HomePage extends React.Component {
constructor(props) {
this.state = {
env: { CUSTOM_VARIABLES }
}
logEnv() {
console.log(this.state.env.variable1)
}
}
本文标签: javascriptRetrieve env variables in Strapi pluginStack Overflow
版权声明:本文标题:javascript - Retrieve env variables in Strapi plugin - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745501238a2661040.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论