【问题标题】:NEXT JS environment variables not working in my local systemNEXT JS 环境变量在我的本地系统中不起作用
【发布时间】:2022-01-26 05:21:00
【问题描述】:

我对 Next.js 比较陌生,由于某种原因,在我的本地系统中我无法访问环境变量。我在这里提供所有相关文件。

pages/index.js

import type {NextPage} from 'next'
import {useEffect} from "react";

const Home: NextPage = (props) => {

    useEffect(()=>{
        console.log("From Client API_URL:", process.env.API_URL)
        console.log("From Client NEXT_PUBLIC_API_URL:", process.env.NEXT_PUBLIC_API_URL)
    }, []);

    return (
        <>
            Hello World!
        </>
    )
}

export async function getServerSideProps() {
    console.log("From Client API_URL:", process.env.API_URL)
    console.log("From Client NEXT_PUBLIC_API_URL:", process.env.NEXT_PUBLIC_API_URL)
    return {
        props: {
            hello: "World!"
        }
    }
}

export default Home

.env.local文件

API_URL:https://fakestoreapi.com/
NEXT_PUBLIC_API_URL:https://fakestoreapi.com/

在浏览器中,我在控制台中得到了这个。

在服务器中,我得到了这个

> npm run dev

> dev
> next dev

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Loaded env from C:\...\next-app\.env.local
warn  - SWC minify beta enabled. https://nextjs.org/docs/messages/swc-minify-enabled
event - compiled client and server successfully in 361 ms (217 modules)
wait  - compiling / (client and server)...
event - compiled client and server successfully in 92 ms (220 modules)
From server API_URL: undefined
From server NEXT_PUBLIC_API_URL: undefined
wait  - compiling /_error (client and server)...
event - compiled client and server successfully in 78 ms (221 modules)

下一个js版本:12.0.7

节点js版本:14.17.1

npm 版本:8.1.4

【问题讨论】:

  • 您打错了,您需要使用= 为您的.env 文件中的环境变量赋值,即API_URL=https://fakestoreapi.com/

标签: node.js reactjs next.js environment-variables


【解决方案1】:

您的.env.local 似乎配置不正确, 您应该在使用 = not 为键赋值时:

.env.local

API_URL=https://fakestoreapi.com/
NEXT_PUBLIC_API_URL=https://fakestoreapi.com/

【讨论】:

    【解决方案2】:

    您无法在 Next.js 中访问客户端的 .env 文件中的密钥,该文件不以 NEXT_PUBLIC_ 开头。

    另外,你的文件应该是 .env 。

    .env

    NEXT_PUBLIC_API_URL:https://fakestoreapi.com/
    

    pages/index.js

    import type {NextPage} from 'next'
    import {useEffect} from "react";
    
    const Home: NextPage = (props) => {
    
        useEffect(()=>{
            console.log("From Client API_URL:", process.env.NEXT_PUBLIC_API_URL)
            console.log("From Client NEXT_PUBLIC_API_URL:", process.env.NEXT_PUBLIC_API_URL)
        }, []);
    
        return (
            <>
                Hello World!
            </>
        )
    }
    
    export async function getServerSideProps() {
        console.log("From Client API_URL:", process.env.NEXT_PUBLIC_API_URL)
        console.log("From Client NEXT_PUBLIC_API_URL:", process.env.NEXT_PUBLIC_API_URL)
        return {
            props: {
                hello: "World!"
            }
        }
    }
    

    导出默认主页

    【讨论】:

    • 我遵守您在我上面的问题中提到的两条规则。尽管如此,我仍然无法访问文件 .env.local 中的 NEXT_PUBLIC_API_URL,根据文档doc
    • @RushiRami 我认为这是正确的:stackoverflow.com/a/70492256/5599770
    猜你喜欢
    • 2015-09-27
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 2015-08-20
    • 2015-09-16
    • 2016-09-23
    • 1970-01-01
    相关资源
    最近更新 更多