You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Azure DevOps部署Azure Function时@prisma/client初始化报错

问题描述

部署基于Node.js+TypeScript的Azure Function到Azure DevOps时,遇到Prisma初始化错误:

@prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.

本地环境Prisma运行正常,未使用Prisma的函数可成功部署。

相关配置

package.json

{  
  "name": "testtsnode",  
  "version": "1.0.0",  
  "description": "",  
  "scripts": {    
    "build": "tsc",    
    "watch": "tsc -w",    
    "prestart": "npx prisma generate && npm run build",    
    "start": "func start",    
    "test": "echo \"No tests yet...\"",    
    "prisma:g": "npx prisma generate",    
    "prisma:s": "npx prisma studio"  
  },  
  "dependencies": {    
    "@prisma/client": "^4.11.0",    
    "mongodb": "^5.1.0",    
    "tsc-silent": "^1.2.2"  
  },  
  "devDependencies": {    
    "@azure/functions": "^3.0.0",    
    "@types/node": "^16.x",    
    "typescript": "^4.0.0"  
  }
}

Azure DevOps YAML配置

pool:  
  name: Azure Pipelines
steps:
- bash: |   
    if [ -f extensions.csproj ]   
    then       
        dotnet build extensions.csproj --output ./bin   
    fi  
  displayName: 'Build extensions'
- task: NodeTool@0  
  displayName: 'Use Node version 18.15.0'  
  inputs:    
    versionSpec: 18.15.0
- task: Npm@1  
  displayName: 'Install Application Dependencies'  
  inputs:    
    workingDir: '$(System.DefaultWorkingDirectory)'    
    verbose: false
- task: Npm@1  
  displayName: 'Run ''build'' script'  
  inputs:    
    command: custom    
    workingDir: '$(System.DefaultWorkingDirectory)'    
    verbose: false    
    customCommand: 'run build --if-present'
- task: Npm@1  
  displayName: 'Remove extraneous packages'  
  inputs:    
    command: custom    
    workingDir: '$(System.DefaultWorkingDirectory)'    
    verbose: false    
    customCommand: 'prune --production'
- task: ArchiveFiles@2  
  displayName: 'Archive files'  
  inputs:    
    rootFolderOrFile: '$(System.DefaultWorkingDirectory)'    
    includeRootFolder: false
- task: AzureFunctionApp@2  
  displayName: 'Azure Function App Deploy'  
  inputs:    
    azureSubscription: DevOps    
    appType: functionAppLinux    
    appName: tsnodefunction20230315    
    package: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'

已尝试在postinstall和prestart中添加npx prisma generate,修改过prisma generate输出路径,均未解决问题。


解决方案

问题核心是部署流程未正确执行prisma generate,或生成的客户端文件未被包含在部署包中,按以下步骤修复:

1. 在DevOps流水线中显式执行prisma generate

当前流水线仅执行了TypeScript构建,未触发Prisma客户端生成。在Run 'build' script任务后、Remove extraneous packages任务前新增Npm任务:

- task: Npm@1  
  displayName: 'Generate Prisma Client'  
  inputs:    
    command: custom    
    workingDir: '$(System.DefaultWorkingDirectory)'    
    verbose: false    
    customCommand: 'run prisma:g'

也可直接执行命令:

- task: Npm@1  
  displayName: 'Generate Prisma Client'  
  inputs:    
    command: custom    
    workingDir: '$(System.DefaultWorkingDirectory)'    
    verbose: false    
    customCommand: 'exec prisma generate'

2. 补充prisma到devDependencies

你的package.json中缺少prisma包,流水线中执行prisma generate会依赖该CLI工具,添加后可避免npx临时下载的不稳定问题:

"devDependencies": {
  "@azure/functions": "^3.0.0",
  "@types/node": "^16.x",
  "typescript": "^4.0.0",
  "prisma": "^4.11.0" // 版本需与@prisma/client一致
}

3. 确保生成的客户端文件被包含在部署包中

  • 检查.funcignore文件,确认未排除node_modules/@prisma/client或自定义的Prisma输出目录;
  • 若修改过prisma generate的输出路径(如指定到src/generated),需确保该目录在TypeScript编译后被纳入输出目录,且未被.funcignore排除。

4. 验证部署包内容(可选)

在ArchiveFiles任务后添加以下任务,确认Prisma客户端文件已被打包:

- bash: |
    unzip -l $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip | grep "@prisma/client"
  displayName: 'Verify Prisma Client in Package'

内容的提问来源于stack exchange,提问作者吳有擇

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 10:52:39