Node.js 如何使用AWS代码构建npm安装所有函数目录

Node.js 如何使用AWS代码构建npm安装所有函数目录,node.js,amazon-web-services,aws-codebuild,Node.js,Amazon Web Services,Aws Codebuild,我正在为我的后端使用AWS Cloud Formation,其项目文件结构如下: | template.yaml | lambda-functions | ---- function-1 |----function.js |----package.json | ---- function-2 |----function.js |----package.json 在AWS buildspec中,我执行AWS cloudformati

我正在为我的后端使用AWS Cloud Formation,其项目文件结构如下:

| template.yaml
| lambda-functions
  | ---- function-1
       |----function.js
       |----package.json
  | ---- function-2
       |----function.js
       |----package.json
在AWS buildspec中,我执行
AWS cloudformation包
,然后是
AWS cloudformation部署

如果我想让它工作,我需要在
function-1
function-2
子文件夹上执行
npm安装
,并将
node\u模块
子文件夹提交到git repo

如何直接从buildspec在我的所有子文件夹上运行npm install,这样我就不必提交节点模块子文件夹?

您可以使用

如果您的包之间存在依赖关系,Lerna也会帮助您

基本上,您只需在根目录中添加一个lerna.json,并使用lerna安装依赖项

lerna.json:

{
  "lerna": "2.11.0",
  "packages": [
    "lambda-functions/*"
  ],
  "version": "0.0.0"
}
version: 0.2
phases:
  install:
    commands:
      - echo Entered the install phase...
      - npm install --global lerna 
      - lerna bootstrap --concurrency=1 -- --production

  ...
version: 0.2
phases:
  install:
    commands:
      - echo Entered the install phase...
      - npm install --global yarn
      - yarn --cwd lambda-functions/function-1 --production install
      - yarn --cwd lambda-functions/function-2 --production install
      - yarn --cwd lambda-functions/function-3 --production install

  ...
version: 0.2
phases:
  install:
    commands:
      - echo Entered the install phase...
      - cd lambda-functions/function-1 && npm install --production
      - cd lambda-functions/function-2 && npm install --production
      - cd lambda-functions/function-3 && npm install --production

  ...
我假设您使用的是AWS CodeBuild,下面是一些关于如何配置安装阶段的示例:

buildspec.yml与lerna:

{
  "lerna": "2.11.0",
  "packages": [
    "lambda-functions/*"
  ],
  "version": "0.0.0"
}
version: 0.2
phases:
  install:
    commands:
      - echo Entered the install phase...
      - npm install --global lerna 
      - lerna bootstrap --concurrency=1 -- --production

  ...
version: 0.2
phases:
  install:
    commands:
      - echo Entered the install phase...
      - npm install --global yarn
      - yarn --cwd lambda-functions/function-1 --production install
      - yarn --cwd lambda-functions/function-2 --production install
      - yarn --cwd lambda-functions/function-3 --production install

  ...
version: 0.2
phases:
  install:
    commands:
      - echo Entered the install phase...
      - cd lambda-functions/function-1 && npm install --production
      - cd lambda-functions/function-2 && npm install --production
      - cd lambda-functions/function-3 && npm install --production

  ...
lerna引导程序
将为每个包创建
node\u模块

如果不想使用lerna,可以为每个包添加一个命令。比如:

buildspec.yml带纱线:

{
  "lerna": "2.11.0",
  "packages": [
    "lambda-functions/*"
  ],
  "version": "0.0.0"
}
version: 0.2
phases:
  install:
    commands:
      - echo Entered the install phase...
      - npm install --global lerna 
      - lerna bootstrap --concurrency=1 -- --production

  ...
version: 0.2
phases:
  install:
    commands:
      - echo Entered the install phase...
      - npm install --global yarn
      - yarn --cwd lambda-functions/function-1 --production install
      - yarn --cwd lambda-functions/function-2 --production install
      - yarn --cwd lambda-functions/function-3 --production install

  ...
version: 0.2
phases:
  install:
    commands:
      - echo Entered the install phase...
      - cd lambda-functions/function-1 && npm install --production
      - cd lambda-functions/function-2 && npm install --production
      - cd lambda-functions/function-3 && npm install --production

  ...
或:

buildspec.yml与npm:

{
  "lerna": "2.11.0",
  "packages": [
    "lambda-functions/*"
  ],
  "version": "0.0.0"
}
version: 0.2
phases:
  install:
    commands:
      - echo Entered the install phase...
      - npm install --global lerna 
      - lerna bootstrap --concurrency=1 -- --production

  ...
version: 0.2
phases:
  install:
    commands:
      - echo Entered the install phase...
      - npm install --global yarn
      - yarn --cwd lambda-functions/function-1 --production install
      - yarn --cwd lambda-functions/function-2 --production install
      - yarn --cwd lambda-functions/function-3 --production install

  ...
version: 0.2
phases:
  install:
    commands:
      - echo Entered the install phase...
      - cd lambda-functions/function-1 && npm install --production
      - cd lambda-functions/function-2 && npm install --production
      - cd lambda-functions/function-3 && npm install --production

  ...

太棒了,我不知道lerna,我会看一看。谢谢你提供的信息,从我收集的信息来看,lerna使用符号链接(不重新安装软件包),如果遇到共享软件包,对吗?如果是这样,这些lambda函数将独立打包和部署,这将如何工作?符号链接的包将不存在。