Skip to main content

环境配置和变量

¥Environment configuration and variables

Strapi 提供了特定的环境变量名称。在环境文件(例如 .env)中定义它们将使这些变量及其值在你的代码中可用。

¥Strapi provides specific environment variable names. Defining them in an environment file (e.g., .env) will make these variables and their values available in your code.

💡 提示

env() 实用程序可用于 检索环境变量的值将变量转换为不同类型

¥An env() utility can be used to retrieve the value of environment variables and cast variables to different types.

此外,可以创建特定的 不同环境的配置

¥Additionally, specific configurations for different environments can be created.

Strapi 的环境变量

¥Strapi's environment variables

Strapi 提供以下环境变量:

¥Strapi provides the following environment variables:

环境描述类型默认值
STRAPI_TELEMETRY_DISABLED不要将遥测使用数据发送到 StrapiBooleanfalse
STRAPI_LICENSE用于激活企业版的许可证密钥Stringundefined
NODE_ENV应用运行的环境类型。

production 启用特定行为(有关详细信息,请参阅 Node.js 文档
String'development'
BROWSER启动后在浏览器中打开管理面板Booleantrue
ENV_PATH包含环境变量的文件的路径String'./.env'
STRAPI_PLUGIN_I18N_INIT_LOCALE_CODE

可选
应用的初始化区域设置(如果已安装 国际化 (i18n) 插件 并在内容类型上启用)(请参阅 生产环境中 i18n 的配置String'en'
STRAPI_ENFORCE_SOURCEMAPS强制打包器发出源映射,这有助于调试管理应用中的错误。booleanfalse
FAST_REFRESH(仅适用于 webpack)
在开发 Strapi 管理面板时,使用 react-refresh 启用 "快速刷新" 以获得近乎即时的反馈。
booleantrue
💡 提示

在环境变量名称中添加 STRAPI_ADMIN_ 前缀会将变量公开给管理前端(例如,可以通过 process.env.STRAPI_ADMIN_MY_PLUGIN_VARIABLE 访问 STRAPI_ADMIN_MY_PLUGIN_VARIABLE)。

¥Prefixing an environment variable name with STRAPI_ADMIN_ exposes the variable to the admin front end (e.g., STRAPI_ADMIN_MY_PLUGIN_VARIABLE is accessible through process.env.STRAPI_ADMIN_MY_PLUGIN_VARIABLE).

环境配置

¥Environment configurations

可以使用以下命名和结构约定创建配置:./config/env/{environment}/{filename}。当你需要针对特定环境的特定静态配置并且使用环境变量不是最佳解决方案时,这非常有用。

¥Configurations can be created with the following naming and structure conventions: ./config/env/{environment}/{filename}. This is useful when you need specific static configurations for specific environments and using environment variables is not the best solution.

这些配置将合并到 ./config 文件夹中定义的基本配置中。该环境基于 NODE_ENV 环境变量,默认为 development

¥These configurations will be merged into the base configurations defined in the ./config folder. The environment is based on the NODE_ENV environment variable, which defaults to development.

当使用 NODE_ENV=production 启动 Strapi 时,它将加载 ./config/*./config/env/production/* 的配置。生产配置中定义的所有内容都将覆盖默认配置。与环境变量相结合,这种模式变得非常强大。

¥When starting Strapi with NODE_ENV=production it will load the configuration from ./config/* and ./config/env/production/*. Everything defined in the production configuration will override the default configuration. In combination with environment variables this pattern becomes really powerful.

例如,使用以下配置文件将为你提供启动服务器的各种选项:

¥For instance, using the following configuration files will give you various options to start the server:

./config/server.js

module.exports = {
host: '127.0.0.1',
};
./config/env/production/server.js

module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
});

使用这些配置文件,服务器将根据传递的环境变量在各个端口上启动:

¥With these configuration files the server will start on various ports depending on the environment variables passed:

yarn start                                   # uses host 127.0.0.1
NODE_ENV=production yarn start # uses host defined in .env. If not defined, uses 0.0.0.0
HOST=10.0.0.1 NODE_ENV=production yarn start # uses host 10.0.0.1