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

如何确保tslib等依赖包被纳入NX生产构建的Package.json?

NX构建NestJS应用时生成的package.json缺失依赖问题

我使用NX构建NestJS应用并通过Pulumi部署到Elastic Beanstalk时,生成的package.json中未包含以下依赖包:

  • tslib
  • class-transformer
  • @nestjs/platform-socket.io

请问是否有方法正确打包这些包或将其自动添加到package.json中?

我的预期是:关闭"generatePackageJson": true时,所有依赖都会被打包;启用该配置时,这些依赖应该出现在生成的package.json中。


相关配置文件

project.json

{  
  "name": "api",  
  "$schema": "../../node_modules/nx/schemas/project-schema.json",  
  "sourceRoot": "apps/api/src",  
  "projectType": "application",  
  "targets": {    
    "build": {      
      "executor": "@nrwl/webpack:webpack",      
      "outputs": ["{options.outputPath}"],      
      "options": {        
        "target": "node",        
        "compiler": "tsc",        
        "outputPath": "dist/apps/api",        
        "main": "apps/api/src/main.ts",        
        "tsConfig": "apps/api/tsconfig.app.json",        
        "assets": ["apps/api/src/assets"],        
        "tsPlugins": [          
          {            
            "name": "@nestjs/swagger/plugin",            
            "options": {              
              "dtoFileNameSuffix": [".entity.ts", ".dto.ts"],              
              "controllerFileNameSuffix": [".controller.ts"],              
              "classValidatorShim": true,              
              "dtoKeyOfComment": "description",              
              "controllerKeyOfComment": "description",              
              "introspectComments": true            
            }          
          }        
        ],        
        "generatePackageJson": true      
      },      
      "configurations": {        
        "production": {          
          "optimization": true,          
          "extractLicenses": true,          
          "inspect": false,          
          "fileReplacements": [            
            {              
              "replace": "apps/api/src/environments/environment.ts",              
              "with": "apps/api/src/environments/environment.prod.ts"            
            }          
          ]        
        }      
      }    
    },    
    "zip": {      
      "executor": "nx:run-commands",      
      "options": {        
        "commands": [          
          "echo 'web: node main.js' > Procfile",          
          "cp -r ../../../libs/shared/prisma .",          
          "cp -r ../../../apps/api/.platform .",          
          "json -I -f package.json -e 'this.dependencies.tslib=\"^2.0.0\"' && zip -r ../api.zip ."        
        ],        
        "cwd": "dist/apps/api"      
      }
    },
    "serve": {      
      "executor": "@nrwl/js:node",      
      "options": {        
        "buildTarget": "api:build"      
      },      
      "configurations": {        
        "production": {          
          "buildTarget": "api:build:production"        
        }      
      }    
    },    
    "lint": {      
      "executor": "@nrwl/linter:eslint",      
      "outputs": ["{options.outputFile}"],      
      "options": {        
        "lintFilePatterns": ["apps/api/**/*.ts"]      
      }    
    },    
    "test": {      
      "executor": "@nrwl/jest:jest",      
      "outputs": ["{workspaceRoot}/coverage/{projectRoot}"],      
      "options": {        
        "jestConfig": "apps/api/jest.config.ts",        
        "passWithNoTests": true      
      }    
    },    
    "deploy": {      
      "executor": "nx:run-commands",      
      "options": {        
        "commands": ["nx run api-infrastructure:up"]      
      }    
    }  
  },  
  "tags": []
}

tsconfig.app.json

{  
  "extends": "./tsconfig.json",  
  "compilerOptions": {    
    "outDir": "../../dist/out-tsc",    
    "module": "commonjs",    
    "types": ["node", "express", "multer"],    
    "emitDecoratorMetadata": true,    
    "target": "es2015"  
  },  
  "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"],  
  "include": ["**/*.ts", "../../libs/shared/**/*.ts"]
}

tsconfig.base.json(根目录配置)

{  
  "compileOnSave": false,  
  "compilerOptions": {    
    "rootDir": ".",    
    "sourceMap": true,    
    "declaration": false,    
    "moduleResolution": "node",    
    "emitDecoratorMetadata": true,    
    "experimentalDecorators": true,    
    "importHelpers": true,    
    "target": "es2015",    
    "module": "esnext",    
    "lib": ["es2017", "dom"],    
    "skipLibCheck": true,    
    "skipDefaultLibCheck": true,    
    "baseUrl": ".",    
    "paths": {      
      "@epicode/aws-components": ["libs/aws-components/src/index.ts"],      
      "@epicode/shared": ["libs/shared/src/index.ts"],      
      "@epicode/shared-ui": ["libs/shared-ui/src/index.ts"],      
      "@epicode/types": ["libs/types/src/index.ts"]    
    }  
  },  
  "exclude": ["node_modules", "tmp"]
}

临时解决方法

目前我采用的临时方案是在main.ts中手动导入这些包:

import 'class-transformer';
import '@nestjs/platform-socket.io';

并在打包部署前手动将它们添加到package.json中。


内容的提问来源于stack exchange,提问作者Ubeyt Demir

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 02:01:16