Find answers from the community

Updated 3 months ago

Missing OPENAI_API_KEY env variable

So I'm trying to run the new and shiny npx create-llama command, and I keep getting this error message for NextJS
Plain Text
(node:20376) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
/Users/tesseract/01-10 Engineering/01 Software Development/01.06 JavaScript/my-app/node_modules/llamaindex/dist/index.js:605
      throw new Error("Set OpenAI Key in OPENAI_API_KEY env variable");
            ^

Error: Set OpenAI Key in OPENAI_API_KEY env variable
    at new OpenAISession (/Users/tesseract/01-10 Engineering/01 Software Development/01.06 JavaScript/my-app/node_modules/llamaindex/dist/index.js:605:13)
    at getOpenAISession (/Users/tesseract/01-10 Engineering/01 Software Development/01.06 JavaScript/my-app/node_modules/llamaindex/dist/index.js:621:15)
    at new OpenAI2 (/Users/tesseract/01-10 Engineering/01 Software Development/01.06 JavaScript/my-app/node_modules/llamaindex/dist/index.js:1540:81)
    at serviceContextFromDefaults (/Users/tesseract/01-10 Engineering/01 Software Development/01.06 JavaScript/my-app/node_modules/llamaindex/dist/index.js:2083:71)
    at file:///Users/tesseract/01-10%20Engineering/01%20Software%20Development/01.06%20JavaScript/my-app/app/api/chat/engine/generate.mjs:41:26
    at file:///Users/tesseract/01-10%20Engineering/01%20Software%20Development/01.06%20JavaScript/my-app/app/api/chat/engine/generate.mjs:48:3
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v21.1.0

Aborting installation.
  npm run generate has failed.
P
A
3 comments
The solution is in the error output.

You have to set the OPENAI_API_KEY environment variable.
I did @Punkbit when it asked me to do so as part of the npx llama-create. But I fixed the issue now by explicitly running export OPENAI_API_KEY = sk-xxxxxxx in the terminal and then ran the command. Now it works.
If you had done it correctly, you wouldn't have asked.

You have the issue fixed now, great!
Add a reply
Sign up and join the conversation on Discord