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

咨询:Lerna+Yarn Workspaces构建完整node_modules(Serverless免NPM发布)

Great question! You absolutely don’t need to publish your local packages to npm to get a fully populated node_modules folder for Serverless deployment—Lerna and Yarn Workspaces have you covered. Here’s a step-by-step breakdown of how to make this work:

Core Approach

We’ll leverage Yarn Workspaces’ native dependency resolution and Lerna’s task management to bundle or prepare your service packages with all required dependencies (including your local workspace packages) without ever pushing to npm.

1. Double-Check Your Workspace Configuration

First, make sure your root package.json is set up correctly to enable workspaces:

{
  "private": true,
  "workspaces": ["packages/*"],
  "scripts": {
    "build": "lerna run build" // Compile all packages if needed
  }
}

Each of your local packages should reference other workspace packages by their name (not an npm version) in their package.json dependencies, like:

{
  "name": "@my-app/api-function",
  "dependencies": {
    "@my-app/shared-helpers": "*" // Uses the local workspace version
  }
}

2. Install Dependencies with Workspace Linking

Run this at your project root to install all dependencies and link your local packages:

yarn install

Yarn will create a root node_modules with symlinks to your local packages, but Serverless platforms usually don’t handle symlinks well. We need to convert these symlinks to actual files for deployment.

3. Prepare Your Package for Deployment (Two Reliable Options)

Option 1: Use yarn pack to Create a Self-Contained Bundle

For the specific package you want to deploy (e.g., @my-app/api-function), navigate to its directory and run:

yarn pack --filename api-function.tgz

This generates a tarball that includes your package’s compiled code plus all resolved dependencies—including the full code of your local @my-app/shared-helpers package, not just a symlink. Extract the tarball into a folder, and you’ll have a complete, symlink-free node_modules ready for Serverless.

Option 2: Use yarn workspaces focus to Install Dependencies Locally

If you prefer a more streamlined workflow, use Yarn’s focus command to install only the dependencies needed for your target package directly into its directory:

yarn workspaces focus @my-app/api-function --production

This creates a node_modules folder right in your function’s directory with all production dependencies (no symlinks—Yarn copies the actual code of your local packages here). This is perfect for Serverless, as it’s a self-contained folder ready to deploy.

4. Integrate with Your Serverless Workflow

You can wrap these steps into scripts to automate deployment. For example, add this to your root package.json:

{
  "scripts": {
    "deploy:api": "yarn build && yarn workspaces focus @my-app/api-function --production && serverless deploy --function apiHandler"
  }
}

Or configure your serverless.yml to include the prepared node_modules and compiled code:

package:
  include:
    - node_modules/**
    - dist/** # Your compiled source code
  exclude:
    - src/** # Exclude uncompiled files

Key Tips

  • Skip npm entirely: All local package resolution happens via Yarn Workspaces, so you never need to publish to npm (even a private registry).
  • Keep bundles small: Use the --production flag to exclude dev dependencies, which reduces deployment size and speeds up cold starts.
  • Avoid symlink issues: Both yarn pack and yarn workspaces focus eliminate symlinks, which is critical for most Serverless platforms that don’t follow symlinks during deployment.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 04:07:20