在根目录之外设置 dotenv 路径不起作用
Setting dotenv path outside of root directory is not working
我想设置dotenv config的路径,但是不行。
我的目录是
myproject/script/.env
myproject/web/src/config.js,
所以我在 config.js 中设置了 dotenv 路径如下,这是行不通的。
import dotenv from 'dotenv'
dotenv.config({ path: '../../scripts/.env'})
是否无法在 web
目录之外设置路径?因为当我将 .env
文件放在 web
目录中时它起作用了。问题是我想使用 script
目录中的另一个 .env
文件,但我做不到。
这样的相对路径是相对于当前工作目录的(尝试使用 console.log(process.cwd())
记录),而不是相对于您所在文件的位置。
要使该路径相对于您的 config.js
,您可以这样做:
dotenv.config({ path: `${__dirname}/../../scripts/.env` })
我想设置dotenv config的路径,但是不行。 我的目录是
myproject/script/.env
myproject/web/src/config.js,
所以我在 config.js 中设置了 dotenv 路径如下,这是行不通的。
import dotenv from 'dotenv'
dotenv.config({ path: '../../scripts/.env'})
是否无法在 web
目录之外设置路径?因为当我将 .env
文件放在 web
目录中时它起作用了。问题是我想使用 script
目录中的另一个 .env
文件,但我做不到。
这样的相对路径是相对于当前工作目录的(尝试使用 console.log(process.cwd())
记录),而不是相对于您所在文件的位置。
要使该路径相对于您的 config.js
,您可以这样做:
dotenv.config({ path: `${__dirname}/../../scripts/.env` })