diff --git a/templates/components/engines/context/generate.mjs b/templates/components/engines/context/generate.mjs
index 8420dd5f81eab52574ec51770cf0e09ccb139563..9334f98e47558b5d451e21143b9eacf5b0287955 100644
--- a/templates/components/engines/context/generate.mjs
+++ b/templates/components/engines/context/generate.mjs
@@ -5,6 +5,8 @@ import {
   VectorStoreIndex,
 } from "llamaindex";
 
+import * as dotenv from "dotenv";
+
 import {
   CHUNK_OVERLAP,
   CHUNK_SIZE,
@@ -12,6 +14,9 @@ import {
   STORAGE_DIR,
 } from "./constants.mjs";
 
+// Load environment variables from local .env file
+dotenv.config();
+
 async function getRuntime(func) {
   const start = Date.now();
   await func();
diff --git a/templates/index.ts b/templates/index.ts
index a9f0998eb2315ee1e2166904e548d14db7fb5b00..2c1f8737706d9f58afb7a269d15c743f2f8f2bc3 100644
--- a/templates/index.ts
+++ b/templates/index.ts
@@ -14,26 +14,14 @@ import {
   TemplateFramework,
 } from "./types";
 
-const envFileNameMap: Record<TemplateFramework, string> = {
-  nextjs: ".env.local",
-  express: ".env",
-  fastapi: ".env",
-};
-
-const createEnvLocalFile = async (
-  root: string,
-  framework: TemplateFramework,
-  openAIKey?: string,
-) => {
+const createEnvLocalFile = async (root: string, openAIKey?: string) => {
   if (openAIKey) {
-    const envFileName = envFileNameMap[framework];
-    if (!envFileName) return;
+    const envFileName = ".env";
     await fs.writeFile(
       path.join(root, envFileName),
       `OPENAI_API_KEY=${openAIKey}\n`,
     );
     console.log(`Created '${envFileName}' file containing OPENAI_API_KEY`);
-    process.env["OPENAI_API_KEY"] = openAIKey;
   }
 };
 
@@ -42,7 +30,16 @@ const copyTestData = async (
   framework: TemplateFramework,
   packageManager?: PackageManager,
   engine?: TemplateEngine,
+  openAIKey?: string,
 ) => {
+  if (framework === "nextjs") {
+    // XXX: This is a hack to make the build for nextjs work with pdf-parse
+    // pdf-parse needs './test/data/05-versions-space.pdf' to exist - can be removed when pdf-parse is removed
+    const srcFile = path.join(__dirname, "components", "data", "101.pdf");
+    const destPath = path.join(root, "test", "data");
+    await fs.mkdir(destPath, { recursive: true });
+    await fs.copyFile(srcFile, path.join(destPath, "05-versions-space.pdf"));
+  }
   if (engine === "context" || framework === "fastapi") {
     const srcPath = path.join(__dirname, "components", "data");
     const destPath = path.join(root, "data");
@@ -54,7 +51,7 @@ const copyTestData = async (
   }
 
   if (packageManager && engine === "context") {
-    if (process.env["OPENAI_API_KEY"]) {
+    if (openAIKey || process.env["OPENAI_API_KEY"]) {
       console.log(
         `\nRunning ${cyan(
           `${packageManager} run generate`,
@@ -226,6 +223,7 @@ const installTSTemplate = async ({
       "tailwind-merge": "^2",
       "@radix-ui/react-slot": "^1",
       "class-variance-authority": "^0.7",
+      clsx: "^1.2.1",
       "lucide-react": "^0.291",
       remark: "^14.0.3",
       "remark-code-import": "^1.2.0",
@@ -313,7 +311,7 @@ export const installTemplate = async (
     // This is a backend, so we need to copy the test data and create the env file.
 
     // Copy the environment file to the target directory.
-    await createEnvLocalFile(props.root, props.framework, props.openAIKey);
+    await createEnvLocalFile(props.root, props.openAIKey);
 
     // Copy test pdf file
     await copyTestData(
@@ -321,6 +319,7 @@ export const installTemplate = async (
       props.framework,
       props.packageManager,
       props.engine,
+      props.openAIKey,
     );
   }
 };
diff --git a/templates/types/simple/nextjs/next.config.app.js b/templates/types/simple/nextjs/next.config.app.js
index 74655207e206e9efa7215e79ea163e8ae2052361..0ff94969142b0c759dfa75ace8ee4300e8860620 100644
--- a/templates/types/simple/nextjs/next.config.app.js
+++ b/templates/types/simple/nextjs/next.config.app.js
@@ -1,5 +1,15 @@
 /** @type {import('next').NextConfig} */
 const nextConfig = {
+  webpack: (config) => {
+    // See https://webpack.js.org/configuration/resolve/#resolvealias
+    config.resolve.alias = {
+      ...config.resolve.alias,
+      sharp$: false,
+      "onnxruntime-node$": false,
+      mongodb$: false,
+    };
+    return config;
+  },
   experimental: {
     serverComponentsExternalPackages: ["llamaindex"],
     outputFileTracingIncludes: {
diff --git a/templates/types/simple/nextjs/next.config.static.js b/templates/types/simple/nextjs/next.config.static.js
index 166b3e67d75ddc6d594b1c3d0960cc51f54b13b9..c5e9ab41f7395aba6fddbfd39ef9c7c776ae9f79 100644
--- a/templates/types/simple/nextjs/next.config.static.js
+++ b/templates/types/simple/nextjs/next.config.static.js
@@ -2,6 +2,16 @@
 const nextConfig = {
   output: "export",
   images: { unoptimized: true },
+  webpack: (config) => {
+    // See https://webpack.js.org/configuration/resolve/#resolvealias
+    config.resolve.alias = {
+      ...config.resolve.alias,
+      sharp$: false,
+      "onnxruntime-node$": false,
+      mongodb$: false,
+    };
+    return config;
+  },
   experimental: {
     serverComponentsExternalPackages: ["llamaindex"],
     outputFileTracingIncludes: {
diff --git a/templates/types/simple/nextjs/package.json b/templates/types/simple/nextjs/package.json
index 990b41c832c69b81883c88b2d078c55bef15b42c..5faf066d60bb3256f245b9f1536c9cf58e2fe2f4 100644
--- a/templates/types/simple/nextjs/package.json
+++ b/templates/types/simple/nextjs/package.json
@@ -9,6 +9,7 @@
   },
   "dependencies": {
     "llamaindex": "0.0.31",
+    "dotenv": "^16.3.1",
     "nanoid": "^5",
     "next": "^13",
     "react": "^18",
@@ -18,11 +19,11 @@
     "@types/node": "^20",
     "@types/react": "^18",
     "@types/react-dom": "^18",
-    "autoprefixer": "^10",
+    "autoprefixer": "^10.1",
     "eslint": "^8",
     "eslint-config-next": "^13",
     "postcss": "^8",
-    "tailwindcss": "^3",
+    "tailwindcss": "^3.3",
     "typescript": "^5"
   }
 }
\ No newline at end of file
diff --git a/templates/types/simple/nextjs/tsconfig.json b/templates/types/simple/nextjs/tsconfig.json
index c7146963787144d4861e149d8d233049b7daefc7..e779aa667e9e83943147b44d06fa930e9175a2b7 100644
--- a/templates/types/simple/nextjs/tsconfig.json
+++ b/templates/types/simple/nextjs/tsconfig.json
@@ -1,7 +1,11 @@
 {
   "compilerOptions": {
     "target": "es5",
-    "lib": ["dom", "dom.iterable", "esnext"],
+    "lib": [
+      "dom",
+      "dom.iterable",
+      "esnext"
+    ],
     "allowJs": true,
     "skipLibCheck": true,
     "strict": true,
@@ -19,9 +23,19 @@
       }
     ],
     "paths": {
-      "@/*": ["./*"]
-    }
+      "@/*": [
+        "./*"
+      ]
+    },
+    "forceConsistentCasingInFileNames": true,
   },
-  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
-  "exclude": ["node_modules"]
-}
+  "include": [
+    "next-env.d.ts",
+    "**/*.ts",
+    "**/*.tsx",
+    ".next/types/**/*.ts"
+  ],
+  "exclude": [
+    "node_modules"
+  ]
+}
\ No newline at end of file
diff --git a/templates/types/streaming/express/README-template.md b/templates/types/streaming/express/README-template.md
index 7ea94ab755535aefd4d5edc9f20a5908d7fedead..0e9d796106725e9d9ebaa653fa5471e807ee1760 100644
--- a/templates/types/streaming/express/README-template.md
+++ b/templates/types/streaming/express/README-template.md
@@ -18,7 +18,7 @@ Then call the express API endpoint `/api/chat` to see the result:
 
 ```
 curl --location 'localhost:8000/api/chat' \
---header 'Content-Type: application/json' \
+--header 'Content-Type: text/plain' \
 --data '{ "messages": [{ "role": "user", "content": "Hello" }] }'
 ```
 
diff --git a/templates/types/streaming/express/package.json b/templates/types/streaming/express/package.json
index 72f127b407a8c7ce675bf4d634a42f57af78bb4c..6af064a8b6915055d556271119335ee341cec65f 100644
--- a/templates/types/streaming/express/package.json
+++ b/templates/types/streaming/express/package.json
@@ -9,7 +9,7 @@
     "dev": "concurrently \"tsup index.ts --format esm --dts --watch\" \"nodemon -q dist/index.js\""
   },
   "dependencies": {
-    "ai": "^2",
+    "ai": "^2.2.5",
     "cors": "^2.8.5",
     "dotenv": "^16.3.1",
     "express": "^4",
@@ -25,4 +25,4 @@
     "tsup": "^7",
     "typescript": "^5"
   }
-}
+}
\ No newline at end of file
diff --git a/templates/types/streaming/nextjs/next.config.app.js b/templates/types/streaming/nextjs/next.config.app.js
index 74655207e206e9efa7215e79ea163e8ae2052361..0ff94969142b0c759dfa75ace8ee4300e8860620 100644
--- a/templates/types/streaming/nextjs/next.config.app.js
+++ b/templates/types/streaming/nextjs/next.config.app.js
@@ -1,5 +1,15 @@
 /** @type {import('next').NextConfig} */
 const nextConfig = {
+  webpack: (config) => {
+    // See https://webpack.js.org/configuration/resolve/#resolvealias
+    config.resolve.alias = {
+      ...config.resolve.alias,
+      sharp$: false,
+      "onnxruntime-node$": false,
+      mongodb$: false,
+    };
+    return config;
+  },
   experimental: {
     serverComponentsExternalPackages: ["llamaindex"],
     outputFileTracingIncludes: {
diff --git a/templates/types/streaming/nextjs/next.config.static.js b/templates/types/streaming/nextjs/next.config.static.js
index 166b3e67d75ddc6d594b1c3d0960cc51f54b13b9..c5e9ab41f7395aba6fddbfd39ef9c7c776ae9f79 100644
--- a/templates/types/streaming/nextjs/next.config.static.js
+++ b/templates/types/streaming/nextjs/next.config.static.js
@@ -2,6 +2,16 @@
 const nextConfig = {
   output: "export",
   images: { unoptimized: true },
+  webpack: (config) => {
+    // See https://webpack.js.org/configuration/resolve/#resolvealias
+    config.resolve.alias = {
+      ...config.resolve.alias,
+      sharp$: false,
+      "onnxruntime-node$": false,
+      mongodb$: false,
+    };
+    return config;
+  },
   experimental: {
     serverComponentsExternalPackages: ["llamaindex"],
     outputFileTracingIncludes: {
diff --git a/templates/types/streaming/nextjs/package.json b/templates/types/streaming/nextjs/package.json
index e9f23201d893320cfdfcbd3fbac9ad48ebbb8d7c..fc5e483e074b76b101f6089ee7850a57530ea3f4 100644
--- a/templates/types/streaming/nextjs/package.json
+++ b/templates/types/streaming/nextjs/package.json
@@ -8,8 +8,9 @@
     "lint": "next lint"
   },
   "dependencies": {
-    "ai": "^2",
+    "ai": "^2.2.5",
     "llamaindex": "0.0.31",
+    "dotenv": "^16.3.1",
     "next": "^13",
     "react": "^18",
     "react-dom": "^18"
@@ -18,11 +19,11 @@
     "@types/node": "^20",
     "@types/react": "^18",
     "@types/react-dom": "^18",
-    "autoprefixer": "^10",
+    "autoprefixer": "^10.1",
     "eslint": "^8",
     "eslint-config-next": "^13",
     "postcss": "^8",
-    "tailwindcss": "^3",
+    "tailwindcss": "^3.3",
     "typescript": "^5"
   }
 }
\ No newline at end of file
diff --git a/templates/types/streaming/nextjs/tsconfig.json b/templates/types/streaming/nextjs/tsconfig.json
index c7146963787144d4861e149d8d233049b7daefc7..e779aa667e9e83943147b44d06fa930e9175a2b7 100644
--- a/templates/types/streaming/nextjs/tsconfig.json
+++ b/templates/types/streaming/nextjs/tsconfig.json
@@ -1,7 +1,11 @@
 {
   "compilerOptions": {
     "target": "es5",
-    "lib": ["dom", "dom.iterable", "esnext"],
+    "lib": [
+      "dom",
+      "dom.iterable",
+      "esnext"
+    ],
     "allowJs": true,
     "skipLibCheck": true,
     "strict": true,
@@ -19,9 +23,19 @@
       }
     ],
     "paths": {
-      "@/*": ["./*"]
-    }
+      "@/*": [
+        "./*"
+      ]
+    },
+    "forceConsistentCasingInFileNames": true,
   },
-  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
-  "exclude": ["node_modules"]
-}
+  "include": [
+    "next-env.d.ts",
+    "**/*.ts",
+    "**/*.tsx",
+    ".next/types/**/*.ts"
+  ],
+  "exclude": [
+    "node_modules"
+  ]
+}
\ No newline at end of file