[英]Can't access gatsby environment variables on the client side
我設置了.env 文件和 gatsby-config.js 如下。
// .env.development
GATSBY_API_URL=https://example.com/api
// gatsby-config.js
console.log(process.env)
...
...
雖然什么時候運行gatsby develop
,它顯示了所有的環境變量,包括GATSBY_API_URL: 'https://example.com/api'
,但是瀏覽器上沒有環境變量。
// client side
console.log(process.env) // => this will return {} empty object
我想我按照https://www.gatsbyjs.org/docs/environment-variables/正如它所說的那樣,並在 var 中添加了GATSBY_
前綴。
我在客戶端看不到 env var 有什么原因嗎?
gatsby info --clipboard
System:
OS: macOS Sierra 10.12.6
CPU: (4) x64 Intel(R) Core(TM) i5-4258U CPU @ 2.40GHz
Shell: 5.2 - /bin/zsh
Binaries:
Node: 11.2.0 - /usr/local/bin/node
Yarn: 1.9.4 - /usr/local/bin/yarn
npm: 6.4.1 - /usr/local/bin/npm
Browsers:
Chrome: 70.0.3538.110
Firefox: 63.0.3
Safari: 12.0.2
npmPackages:
gatsby: ^2.0.61 => 2.0.61
gatsby-image: ^2.0.22 => 2.0.22
gatsby-plugin-google-analytics: ^2.0.8 => 2.0.8
gatsby-plugin-manifest: ^2.0.11 => 2.0.11
gatsby-plugin-no-sourcemaps: ^2.0.1 => 2.0.1
gatsby-plugin-nprogress: ^2.0.7 => 2.0.7
gatsby-plugin-react-helmet: ^3.0.4 => 3.0.4
gatsby-plugin-sass: ^2.0.5 => 2.0.5
gatsby-plugin-sharp: ^2.0.14 => 2.0.14
gatsby-plugin-styled-components: ^3.0.4 => 3.0.4
gatsby-plugin-typescript: ^2.0.2 => 2.0.2
gatsby-plugin-typography: ^2.2.2 => 2.2.2
gatsby-plugin-webpack-bundle-analyzer: ^1.0.3 => 1.0.3
gatsby-source-filesystem: ^2.0.10 => 2.0.10
gatsby-transformer-sharp: ^2.1.9 => 2.1.9
gatsby-transformer-yaml: ^2.1.6 => 2.1.6
npmGlobalPackages:
gatsby-cli: 2.4.5
我如下設置了.env文件和gatsby-config.js。
// .env.development
GATSBY_API_URL=https://example.com/api
// gatsby-config.js
console.log(process.env)
...
...
盡管何時運行gatsby develop
,它會顯示包括GATSBY_API_URL: 'https://example.com/api'
在內的所有環境變量GATSBY_API_URL: 'https://example.com/api'
,但瀏覽器上沒有環境變量。
// client side
console.log(process.env) // => this will return {} empty object
我認為我遵循了https://www.gatsbyjs.org/docs/environment-variables/ ,並在GATSBY_
添加了GATSBY_
前綴。
為什么在客戶端看不到env var是有原因的?
gatsby info --clipboard
System:
OS: macOS Sierra 10.12.6
CPU: (4) x64 Intel(R) Core(TM) i5-4258U CPU @ 2.40GHz
Shell: 5.2 - /bin/zsh
Binaries:
Node: 11.2.0 - /usr/local/bin/node
Yarn: 1.9.4 - /usr/local/bin/yarn
npm: 6.4.1 - /usr/local/bin/npm
Browsers:
Chrome: 70.0.3538.110
Firefox: 63.0.3
Safari: 12.0.2
npmPackages:
gatsby: ^2.0.61 => 2.0.61
gatsby-image: ^2.0.22 => 2.0.22
gatsby-plugin-google-analytics: ^2.0.8 => 2.0.8
gatsby-plugin-manifest: ^2.0.11 => 2.0.11
gatsby-plugin-no-sourcemaps: ^2.0.1 => 2.0.1
gatsby-plugin-nprogress: ^2.0.7 => 2.0.7
gatsby-plugin-react-helmet: ^3.0.4 => 3.0.4
gatsby-plugin-sass: ^2.0.5 => 2.0.5
gatsby-plugin-sharp: ^2.0.14 => 2.0.14
gatsby-plugin-styled-components: ^3.0.4 => 3.0.4
gatsby-plugin-typescript: ^2.0.2 => 2.0.2
gatsby-plugin-typography: ^2.2.2 => 2.2.2
gatsby-plugin-webpack-bundle-analyzer: ^1.0.3 => 1.0.3
gatsby-source-filesystem: ^2.0.10 => 2.0.10
gatsby-transformer-sharp: ^2.1.9 => 2.1.9
gatsby-transformer-yaml: ^2.1.6 => 2.1.6
npmGlobalPackages:
gatsby-cli: 2.4.5
我如下設置了.env文件和gatsby-config.js。
// .env.development
GATSBY_API_URL=https://example.com/api
// gatsby-config.js
console.log(process.env)
...
...
盡管何時運行gatsby develop
,它會顯示包括GATSBY_API_URL: 'https://example.com/api'
在內的所有環境變量GATSBY_API_URL: 'https://example.com/api'
,但瀏覽器上沒有環境變量。
// client side
console.log(process.env) // => this will return {} empty object
我認為我遵循了https://www.gatsbyjs.org/docs/environment-variables/ ,並在GATSBY_
添加了GATSBY_
前綴。
為什么在客戶端看不到env var是有原因的?
gatsby info --clipboard
System:
OS: macOS Sierra 10.12.6
CPU: (4) x64 Intel(R) Core(TM) i5-4258U CPU @ 2.40GHz
Shell: 5.2 - /bin/zsh
Binaries:
Node: 11.2.0 - /usr/local/bin/node
Yarn: 1.9.4 - /usr/local/bin/yarn
npm: 6.4.1 - /usr/local/bin/npm
Browsers:
Chrome: 70.0.3538.110
Firefox: 63.0.3
Safari: 12.0.2
npmPackages:
gatsby: ^2.0.61 => 2.0.61
gatsby-image: ^2.0.22 => 2.0.22
gatsby-plugin-google-analytics: ^2.0.8 => 2.0.8
gatsby-plugin-manifest: ^2.0.11 => 2.0.11
gatsby-plugin-no-sourcemaps: ^2.0.1 => 2.0.1
gatsby-plugin-nprogress: ^2.0.7 => 2.0.7
gatsby-plugin-react-helmet: ^3.0.4 => 3.0.4
gatsby-plugin-sass: ^2.0.5 => 2.0.5
gatsby-plugin-sharp: ^2.0.14 => 2.0.14
gatsby-plugin-styled-components: ^3.0.4 => 3.0.4
gatsby-plugin-typescript: ^2.0.2 => 2.0.2
gatsby-plugin-typography: ^2.2.2 => 2.2.2
gatsby-plugin-webpack-bundle-analyzer: ^1.0.3 => 1.0.3
gatsby-source-filesystem: ^2.0.10 => 2.0.10
gatsby-transformer-sharp: ^2.1.9 => 2.1.9
gatsby-transformer-yaml: ^2.1.6 => 2.1.6
npmGlobalPackages:
gatsby-cli: 2.4.5
我如下設置了.env文件和gatsby-config.js。
// .env.development
GATSBY_API_URL=https://example.com/api
// gatsby-config.js
console.log(process.env)
...
...
盡管何時運行gatsby develop
,它會顯示包括GATSBY_API_URL: 'https://example.com/api'
在內的所有環境變量GATSBY_API_URL: 'https://example.com/api'
,但瀏覽器上沒有環境變量。
// client side
console.log(process.env) // => this will return {} empty object
我認為我遵循了https://www.gatsbyjs.org/docs/environment-variables/ ,並在GATSBY_
添加了GATSBY_
前綴。
為什么在客戶端看不到env var是有原因的?
gatsby info --clipboard
System:
OS: macOS Sierra 10.12.6
CPU: (4) x64 Intel(R) Core(TM) i5-4258U CPU @ 2.40GHz
Shell: 5.2 - /bin/zsh
Binaries:
Node: 11.2.0 - /usr/local/bin/node
Yarn: 1.9.4 - /usr/local/bin/yarn
npm: 6.4.1 - /usr/local/bin/npm
Browsers:
Chrome: 70.0.3538.110
Firefox: 63.0.3
Safari: 12.0.2
npmPackages:
gatsby: ^2.0.61 => 2.0.61
gatsby-image: ^2.0.22 => 2.0.22
gatsby-plugin-google-analytics: ^2.0.8 => 2.0.8
gatsby-plugin-manifest: ^2.0.11 => 2.0.11
gatsby-plugin-no-sourcemaps: ^2.0.1 => 2.0.1
gatsby-plugin-nprogress: ^2.0.7 => 2.0.7
gatsby-plugin-react-helmet: ^3.0.4 => 3.0.4
gatsby-plugin-sass: ^2.0.5 => 2.0.5
gatsby-plugin-sharp: ^2.0.14 => 2.0.14
gatsby-plugin-styled-components: ^3.0.4 => 3.0.4
gatsby-plugin-typescript: ^2.0.2 => 2.0.2
gatsby-plugin-typography: ^2.2.2 => 2.2.2
gatsby-plugin-webpack-bundle-analyzer: ^1.0.3 => 1.0.3
gatsby-source-filesystem: ^2.0.10 => 2.0.10
gatsby-transformer-sharp: ^2.1.9 => 2.1.9
gatsby-transformer-yaml: ^2.1.6 => 2.1.6
npmGlobalPackages:
gatsby-cli: 2.4.5
我如下設置了.env文件和gatsby-config.js。
// .env.development
GATSBY_API_URL=https://example.com/api
// gatsby-config.js
console.log(process.env)
...
...
盡管何時運行gatsby develop
,它會顯示包括GATSBY_API_URL: 'https://example.com/api'
在內的所有環境變量GATSBY_API_URL: 'https://example.com/api'
,但瀏覽器上沒有環境變量。
// client side
console.log(process.env) // => this will return {} empty object
我認為我遵循了https://www.gatsbyjs.org/docs/environment-variables/ ,並在GATSBY_
添加了GATSBY_
前綴。
為什么在客戶端看不到env var是有原因的?
gatsby info --clipboard
System:
OS: macOS Sierra 10.12.6
CPU: (4) x64 Intel(R) Core(TM) i5-4258U CPU @ 2.40GHz
Shell: 5.2 - /bin/zsh
Binaries:
Node: 11.2.0 - /usr/local/bin/node
Yarn: 1.9.4 - /usr/local/bin/yarn
npm: 6.4.1 - /usr/local/bin/npm
Browsers:
Chrome: 70.0.3538.110
Firefox: 63.0.3
Safari: 12.0.2
npmPackages:
gatsby: ^2.0.61 => 2.0.61
gatsby-image: ^2.0.22 => 2.0.22
gatsby-plugin-google-analytics: ^2.0.8 => 2.0.8
gatsby-plugin-manifest: ^2.0.11 => 2.0.11
gatsby-plugin-no-sourcemaps: ^2.0.1 => 2.0.1
gatsby-plugin-nprogress: ^2.0.7 => 2.0.7
gatsby-plugin-react-helmet: ^3.0.4 => 3.0.4
gatsby-plugin-sass: ^2.0.5 => 2.0.5
gatsby-plugin-sharp: ^2.0.14 => 2.0.14
gatsby-plugin-styled-components: ^3.0.4 => 3.0.4
gatsby-plugin-typescript: ^2.0.2 => 2.0.2
gatsby-plugin-typography: ^2.2.2 => 2.2.2
gatsby-plugin-webpack-bundle-analyzer: ^1.0.3 => 1.0.3
gatsby-source-filesystem: ^2.0.10 => 2.0.10
gatsby-transformer-sharp: ^2.1.9 => 2.1.9
gatsby-transformer-yaml: ^2.1.6 => 2.1.6
npmGlobalPackages:
gatsby-cli: 2.4.5
我如下設置了.env文件和gatsby-config.js。
// .env.development
GATSBY_API_URL=https://example.com/api
// gatsby-config.js
console.log(process.env)
...
...
盡管何時運行gatsby develop
,它會顯示包括GATSBY_API_URL: 'https://example.com/api'
在內的所有環境變量GATSBY_API_URL: 'https://example.com/api'
,但瀏覽器上沒有環境變量。
// client side
console.log(process.env) // => this will return {} empty object
我認為我遵循了https://www.gatsbyjs.org/docs/environment-variables/ ,並在GATSBY_
添加了GATSBY_
前綴。
為什么在客戶端看不到env var是有原因的?
gatsby info --clipboard
System:
OS: macOS Sierra 10.12.6
CPU: (4) x64 Intel(R) Core(TM) i5-4258U CPU @ 2.40GHz
Shell: 5.2 - /bin/zsh
Binaries:
Node: 11.2.0 - /usr/local/bin/node
Yarn: 1.9.4 - /usr/local/bin/yarn
npm: 6.4.1 - /usr/local/bin/npm
Browsers:
Chrome: 70.0.3538.110
Firefox: 63.0.3
Safari: 12.0.2
npmPackages:
gatsby: ^2.0.61 => 2.0.61
gatsby-image: ^2.0.22 => 2.0.22
gatsby-plugin-google-analytics: ^2.0.8 => 2.0.8
gatsby-plugin-manifest: ^2.0.11 => 2.0.11
gatsby-plugin-no-sourcemaps: ^2.0.1 => 2.0.1
gatsby-plugin-nprogress: ^2.0.7 => 2.0.7
gatsby-plugin-react-helmet: ^3.0.4 => 3.0.4
gatsby-plugin-sass: ^2.0.5 => 2.0.5
gatsby-plugin-sharp: ^2.0.14 => 2.0.14
gatsby-plugin-styled-components: ^3.0.4 => 3.0.4
gatsby-plugin-typescript: ^2.0.2 => 2.0.2
gatsby-plugin-typography: ^2.2.2 => 2.2.2
gatsby-plugin-webpack-bundle-analyzer: ^1.0.3 => 1.0.3
gatsby-source-filesystem: ^2.0.10 => 2.0.10
gatsby-transformer-sharp: ^2.1.9 => 2.1.9
gatsby-transformer-yaml: ^2.1.6 => 2.1.6
npmGlobalPackages:
gatsby-cli: 2.4.5
檢查官方文檔。
Gatsby 要求我們使用文件名分別為.env.development
和.env.production
進行開發和生產。
在gatsby-config.js
中,
require("dotenv").config({
path: `.env.${process.env.NODE_ENV}`,
})
如果您使用 typescript (即gatsby-config.ts
),
import * as dotenv from 'dotenv'
dotenv.config({ path: __dirname + `/.env.${process.env.NODE_ENV}` })
如果您打算使用單個.env
文件(而不是.env.development
和.env.production
類的文件),那么將其添加到gatsby-config.js
應該可以解決問題:
require('dotenv').config({
path: '.env',
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.