您应该能够在NextJS中使用process.env.<VARIABLE_NAME>
访问env变量。
如果这对你不起作用,请分享你所做的一切以及结果。
读取.env
关键行:connection: process.env.DB_URL
import dotenv from 'dotenv' dotenv.config() const Config = { client: 'pg', connection: process.env.DB_URL, acquireConnectionTimeout: 5000, pool: { min: 2, // Minimum number of connections in the pool max: 20, // Maximum number of connections in the pool // propagateCreateError: false, // enabling knex to automatically reconnect on create connection failure instead of throwing the error. }, migrations: { directory: './migrations', }, seeds: { directory: './seeds' }, } const knexConfig = { Dev: Config, Beta: Config, Prod: Config } export default knexConfig
读取.env.local
关键行:connection: process.env.local.DB_URL
import dotenv from 'dotenv'
dotenv.config()
const Config = {
client: 'pg',
connection: process.env.local.DB_URL,
acquireConnectionTimeout: 5000,
pool: {
min: 2, // Minimum number of connections in the pool
max: 20, // Maximum number of connections in the pool
// propagateCreateError: false, // enabling knex to automatically reconnect on create connection failure instead of throwing the error.
},
migrations: {
directory: './migrations',
},
seeds: { directory: './seeds' },
}
const knexConfig = { Dev: Config, Beta: Config, Prod: Config }
export default knexConfig
在 Vue中使用 env,底层原理还是 vue-cli-service 内部集成了对 .env 配置文件的处理,并加载到 process.env 上。
加载文件优先级: .env.development.local > .env.development > .env;
可手动指定mode。
只有 NODE_ENV,BASE_URL 和以 VUE_APP_ 开头的变量才会被读取。