Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] 执行pnpm build的后续脚本获取不到.env里面的环境变量从而导致build-migrate-db脚本失败 #5100

Open
xiangnanscu opened this issue Dec 20, 2024 · 6 comments
Labels
🐛 Bug Something isn't working | 缺陷

Comments

@xiangnanscu
Copy link

📦 部署环境

Other

📌 软件版本

Latest

💻 系统环境

Ubuntu

🌐 浏览器

Chrome

🐛 问题描述

目前package.json里面是这样定义的:

    "build": "next build",
    "postbuild": "npm run build-sitemap && npm run build-migrate-db",

📷 复现步骤

执行这个命令后续脚本会出错

pnpm build

克隆仓库到wsl2上之后, 执行pnpm build, 执行完了之后触发postbuild, 再执行build-migrate-db就出错了:

$ pnpm build

> @lobehub/[email protected] build /workspaces/lobe-chat
> next build

Attention: Next.js now collects completely anonymous telemetry regarding usage.
This information is used to shape Next.js' roadmap and prioritize features.
You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
https://nextjs.org/telemetry

  ▲ Next.js 14.2.8
  - Environments: .env

   Creating an optimized production build ...
 ○ (serwist) The service worker will not be automatically registered, please call 'window.serwist.register()' in 'componentDidMount' or 'useEffect'.
 ✓ (serwist) Bundling the service worker script with the URL '/sw.js' and the scope '/'...
 ✓ Compiled successfully

./src/database/server/models/session.ts
304:13  Warning: Empty block statement.  no-empty

info  - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/basic-features/eslint#disabling-rules
 ✓ Linting and checking validity of types    
 ⚠ Using edge runtime on a page currently disables static generation for that page
 ✓ Collecting page data    
 ✓ Generating static pages (103/103)
 ✓ Collecting build traces    
 ✓ Finalizing page optimization    

Route (app)                                   Size     First Load JS
┌ ƒ /                                         13.1 kB         437 kB
├ ƒ /_not-found                               171 B           106 kB
├ ƒ /(.)settings/modal                        5.19 kB         142 kB
├ ƒ /api/auth/[...nextauth]                   0 B                0 B
├ ƒ /api/auth/error                           1.23 kB         107 kB
├ ƒ /api/webhooks/casdoor                     0 B                0 B
├ ƒ /api/webhooks/clerk                       0 B                0 B
├ ƒ /api/webhooks/logto                       0 B                0 B
├ ƒ /chat                                     18.2 kB         616 kB
├ ƒ /chat/(.)settings/modal                   5.06 kB         142 kB
├ ƒ /chat/settings                            10.2 kB        1.48 MB
├ ƒ /chat/settings/modal                      3.1 kB          109 kB
├ ƒ /discover                                 7.2 kB          525 kB
├ ƒ /discover/assistant/[slug]                41.2 kB         903 kB
├ ƒ /discover/assistants                      213 B           344 kB
├ ● /discover/assistants/[slug]               213 B           344 kB
├   ├ /discover/assistants/academic
├   ├ /discover/assistants/all
├   ├ /discover/assistants/career
├   └ [+12 more paths]
├ ƒ /discover/model/[...slugs]                27.1 kB         630 kB
├ ƒ /discover/models                          202 B           498 kB
├ ● /discover/models/[slug]                   202 B           498 kB
├   ├ /discover/models/openai
├   ├ /discover/models/ollama
├   ├ /discover/models/anthropic
├   └ [+33 more paths]
├ ƒ /discover/plugin/[slug]                   34.4 kB         677 kB
├ ƒ /discover/plugins                         210 B           342 kB
├ ● /discover/plugins/[slug]                  210 B           342 kB
├   ├ /discover/plugins/all
├   ├ /discover/plugins/gaming-entertainment
├   ├ /discover/plugins/lifestyle
├   └ [+6 more paths]
├ ƒ /discover/provider/[slug]                 184 kB         1.61 MB
├ ƒ /discover/providers                       8.08 kB         491 kB
├ ƒ /discover/search                          10.5 kB         596 kB
├ ƒ /files                                    628 B           643 kB
├ ƒ /files/(.)[id]                            7.49 kB         816 kB
├ ƒ /files/[id]                               1.04 kB         806 kB
├ ƒ /login/[[...login]]                       2.84 kB         136 kB
├ ƒ /manifest.webmanifest                     0 B                0 B
├ ƒ /me                                       15.1 kB         432 kB
├ ƒ /me/data                                  14.1 kB         858 kB
├ ƒ /me/profile                               6.74 kB         377 kB
├ ƒ /me/settings                              4.68 kB         261 kB
├ ƒ /profile/[[...slugs]]                     2.35 kB         246 kB
├ ƒ /repos                                    170 B           106 kB
├ ƒ /repos/[id]                               317 B           643 kB
├ ƒ /repos/[id]/evals                         171 B           106 kB
├ ƒ /repos/[id]/evals/dataset                 6.75 kB         906 kB
├ ƒ /repos/[id]/evals/evaluation              7.98 kB         851 kB
├ ○ /robots.txt                               0 B                0 B
├ ƒ /settings/about                           20.8 kB         446 kB
├ ƒ /settings/agent                           10.1 kB        1.48 MB
├ ƒ /settings/common                          42.2 kB         715 kB
├ ƒ /settings/llm                             37.7 kB        1.27 MB
├ ƒ /settings/modal                           4.15 kB         110 kB
├ ƒ /settings/sync                            23 kB           485 kB
├ ƒ /settings/system-agent                    25.1 kB         674 kB
├ ƒ /settings/tts                             17.6 kB         666 kB
├ ƒ /signup/[[...signup]]                     2.84 kB         136 kB
├ ● /sitemap/[__metadata_id__]                0 B                0 B
├   ├ /sitemap/pages.xml
├   ├ /sitemap/assistants.xml
├   ├ /sitemap/plugins.xml
├   └ [+2 more paths]
├ ƒ /trpc/async/[trpc]                        0 B                0 B
├ ƒ /trpc/edge/[trpc]                         0 B                0 B
├ ƒ /trpc/lambda/[trpc]                       0 B                0 B
├ ƒ /trpc/tools/[trpc]                        0 B                0 B
├ ƒ /webapi/assistant/[id]                    0 B                0 B
├ ƒ /webapi/assistant/store                   0 B                0 B
├ ƒ /webapi/chat/[provider]                   0 B                0 B
├ ƒ /webapi/chat/anthropic                    0 B                0 B
├ ƒ /webapi/chat/google                       0 B                0 B
├ ƒ /webapi/chat/minimax                      0 B                0 B
├ ƒ /webapi/chat/models/[provider]            0 B                0 B
├ ƒ /webapi/chat/openai                       0 B                0 B
├ ƒ /webapi/chat/wenxin                       0 B                0 B
├ ƒ /webapi/plugin/gateway                    0 B                0 B
├ ƒ /webapi/plugin/store                      0 B                0 B
├ ƒ /webapi/proxy                             0 B                0 B
├ ƒ /webapi/stt/openai                        0 B                0 B
├ ƒ /webapi/text-to-image/[provider]          0 B                0 B
├ ƒ /webapi/tokenizer                         0 B                0 B
├ ƒ /webapi/trace                             0 B                0 B
├ ƒ /webapi/tts/edge                          0 B                0 B
├ ƒ /webapi/tts/microsoft                     0 B                0 B
└ ƒ /webapi/tts/openai                        0 B                0 B
+ First Load JS shared by all                 105 kB
  ├ chunks/21912-1064f0efddef2ad9.js          33 kB
  ├ chunks/e497a044-f3b85692eb274770.js       53.6 kB
  ├ chunks/webpack-42d74809f69399be.js        18.6 kB
  └ other shared chunks (total)               235 B


ƒ Middleware                                  124 kB

○  (Static)   prerendered as static content
●  (SSG)      prerendered as static HTML (uses getStaticProps)
ƒ  (Dynamic)  server-rendered on demand


> @lobehub/[email protected] postbuild /workspaces/lobe-chat
> npm run build-sitemap && npm run build-migrate-db


> @lobehub/[email protected] build-sitemap
> tsx ./scripts/buildSitemapIndex/index.ts


> @lobehub/[email protected] build-migrate-db
> bun run db:migrate

$ MIGRATION_DB=1 tsx ./scripts/migrateServerDB/index.ts
❌ Database migrate failed: TypeError: Cannot read properties of undefined (reading 'migrate')
    at migrate (/workspaces/lobe-chat/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]_5pu7i2tu2hxsw7cluzudcwqn54/node_modules/src/node-postgres/migrator.ts:10:19)
    at runMigrations (/workspaces/lobe-chat/scripts/migrateServerDB/index.ts:17:11)
    at neonMigrate (/workspaces/lobe-chat/scripts/migrateServerDB/index.ts:32:3)
    at Object.<anonymous> (/workspaces/lobe-chat/scripts/migrateServerDB/index.ts:48:1)
    at Module._compile (node:internal/modules/cjs/loader:1469:14)
    at Object.transformer (/workspaces/lobe-chat/node_modules/.pnpm/[email protected]/node_modules/tsx/dist/register-DCnOAxY2.cjs:2:1186)
    at Module.load (node:internal/modules/cjs/loader:1288:32)
    at Module._load (node:internal/modules/cjs/loader:1104:12)
    at cjsLoader (node:internal/modules/esm/translators:346:17)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:286:7)
------------------------------------------------------------------------------------------
⚠️ Database migrate failed due to not find the db instance.

1) You might not switch to server db mode, please set the env blow and try again:

```
NEXT_PUBLIC_SERVICE_MODE=server
```

2) if you are using docker postgres image, you may need to set DATABASE_DRIVER to node

```
DATABASE_DRIVER=node
```

if you have any other question, please open issue here: https://github.com/lobehub/lobe-chat/issues

error: script "db:migrate" exited with code 1
 ELIFECYCLE  Command failed with exit code 1.

🚦 期望结果

不要出错撒

📝 补充信息

No response

@xiangnanscu xiangnanscu added the 🐛 Bug Something isn't working | 缺陷 label Dec 20, 2024
@github-project-automation github-project-automation bot moved this to Roadmap - Chat 1.x in Lobe Chat Routine Dec 20, 2024
@lobehubbot
Copy link
Member

👀 @xiangnanscu

Thank you for raising an issue. We will investigate into the matter and get back to you as soon as possible.
Please make sure you have given us as much context as possible.
非常感谢您提交 issue。我们会尽快调查此事,并尽快回复您。 请确保您已经提供了尽可能多的背景信息。

Copy link

dosubot bot commented Dec 20, 2024

To resolve the issue where the build-migrate-db script fails to access environment variables from the .env file after running pnpm build, ensure that the .env file is correctly loaded by the script. The script uses dotenv.config() to load environment variables, so the .env file must be in the correct location and accessible when the script is executed.

Here are some steps to troubleshoot and resolve the issue:

  1. Check .env File Location: Ensure that the .env file is located in the root directory of your project or the directory where the script is executed. The dotenv.config() function loads the .env file from the current working directory by default.

  2. Verify Environment Variables: Make sure that the .env file contains the necessary environment variables, such as DATABASE_URL, and that they are correctly formatted.

  3. Build Process: If the build process (pnpm build) changes the directory structure or moves files, ensure that the .env file is still accessible in the expected location when the script runs.

  4. Custom Path: If the .env file is not in the default location, you can specify a custom path using the dotenv_config_path parameter when running the Node.js script. For example:

    node -r dotenv/config your-script.js dotenv_config_path=/path/to/your/.env
  5. Check for Errors: If there are any errors during the execution of the script, they will be logged to the console. Review these logs to identify any issues related to environment variable loading.

  6. Environment-Specific Variables: As seen in the drizzle.config.ts file, ensure that your script correctly handles different environments. For example, if you are running tests, the script should switch to using DATABASE_TEST_URL if NODE_ENV is set to test. Ensure that these environment-specific variables are also defined in your .env file [1][2].

By ensuring that the .env file is correctly located and accessible, and that environment-specific variables are handled properly, the script should be able to load the environment variables successfully.

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other

@xiangnanscu
Copy link
Author

说明: .env文件里面我设置了相关信息的:

NEXT_PUBLIC_SERVICE_MODE=server
DATABASE_DRIVER=node

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Note: I have set relevant information in the .env file:

NEXT_PUBLIC_SERVICE_MODE=server
DATABASE_DRIVER=node

@arvinxx
Copy link
Contributor

arvinxx commented Dec 22, 2024

换成 bun 应该就行吧?

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


It should be OK to replace it with bun, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug Something isn't working | 缺陷
Projects
Status: Roadmap - Chat 1.x
Development

No branches or pull requests

3 participants