CircleCI 没有运行“npm run”命令

Posted

技术标签:

【中文标题】CircleCI 没有运行“npm run”命令【英文标题】:CircleCI is not running 'npm run' commands 【发布时间】:2021-11-27 09:17:10 【问题描述】:

我在使用 CircleCI 实施 CI/CD 管道时遇到问题。我无法在管道中执行任何“npm run”命令,而这些命令在我的本地系统上运行。

我已经构建了一个 Angular 客户端和 Node/Express API。我的文件夹结构是通过以下方式设置的。

 - .circleci
   -- config.yml 
 - client
 - server
 - package.json

在管道中,“cd client npm install”有效,而“cd client npm run build”无效。我的 Angular 项目有 'build' 脚本,命令设置为 'ng build'。

但 CircleCI 中的日志仍然显示 'npm ERR!缺少脚本:“构建”'

我的服务器文件夹命令也是如此。另一个值得注意的有趣的事情是,如果我创建一个名为“installapp”的脚本并使用命令“npm install”作为它的值,那么日志现在将显示“npm ERR!缺少脚本:“installapp”'。所以“npm install”有效,但“npm run installapp”无效,即使它们最终都执行了“npm install”。

有人可以帮我解决这个问题吗?我正在共享我的 config.yml 和所有 package.json 文件以供参考以帮助解决此错误。

.circleci/config.yml

version: 2.1
orbs:
  node: circleci/node@4.1.0
  aws-cli: circleci/aws-cli@1.3.1
jobs:
  build:
    docker:
      - image: "cimg/base:stable"
    steps:
      - node/install
      - checkout
      - aws-cli/setup
      - run:
          name: Front-End Install
          command: |
            npm run frontend:install
      - run:
          name: Back-End Install
          command: |
            npm run backend:install
      - run:
          name: Front-End Build
          command: |
            npm run frontend:build
      - run:
          name: Back-End Build
          command: |
            npm run backend:build
      - run:
          name: Deploy App
          command: |
            npm run frontend:deploy
            npm run backend:deploy

根应用的 package.json


    "scripts": 
        "frontend:install": "cd client && npm install",
        "backend:install": "cd server && npm install",
        "frontend:build": "cd client && npm run build",
        "backend:build": "cd server && npm run build",
        "frontend:deploy": "cd client && npm run deploy",
        "backend:deploy": "cd server && npm run deploy"
    

客户端 package.json (Angular)


  "name": "guitar-store-app",
  "version": "0.0.0",
  "scripts": 
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test",
    "lint": "ng lint",
    "deploy": "chmod +x ./bin/deploy.sh && ./bin/deploy.sh"
  ,
  "private": true,
  "dependencies": 
    "@angular/animations": "~12.2.0",
    "@angular/common": "~12.2.0",
    "@angular/compiler": "~12.2.0",
    "@angular/core": "~12.2.0",
    "@angular/forms": "~12.2.0",
    "@angular/platform-browser": "~12.2.0",
    "@angular/platform-browser-dynamic": "~12.2.0",
    "@angular/router": "~12.2.0",
    "@auth0/angular-jwt": "^5.0.2",
    "rxjs": "~6.6.0",
    "tslib": "^2.3.0",
    "w3-css": "^4.1.0",
    "zone.js": "~0.11.4"
  ,
  "devDependencies": 
    "@angular-devkit/build-angular": "~12.2.1",
    "@angular-eslint/builder": "12.3.1",
    "@angular-eslint/eslint-plugin": "12.3.1",
    "@angular-eslint/eslint-plugin-template": "12.3.1",
    "@angular-eslint/schematics": "12.3.1",
    "@angular-eslint/template-parser": "12.3.1",
    "@angular/cli": "~12.2.1",
    "@angular/compiler-cli": "~12.2.0",
    "@types/jasmine": "~3.8.0",
    "@types/node": "^12.11.1",
    "@typescript-eslint/eslint-plugin": "4.28.2",
    "@typescript-eslint/parser": "4.28.2",
    "eslint": "^7.26.0",
    "jasmine-core": "~3.8.0",
    "karma": "~6.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "~1.7.0",
    "typescript": "~4.3.5"
  

服务器 package.json (Node/Express)


  "name": "storefront-backend-api",
  "version": "1.0.0",
  "description": "An e-commerce API",
  "main": "build/server/app.js",
  "engines": 
    "node": ">=14.17.1",
    "npm": ">=6.14.13"
  ,
  "scripts": 
    "lint": "eslint src/**/*.ts",
    "prettier": "prettier --config .prettierrc src/**/*.ts --write",
    "build": "npx tsc",
    "jasmine": "jasmine",
    "test-local": "set ENV=test&& db-migrate db:create guitar_shop_test && db-migrate --env test up && npm run jasmine && db-migrate db:drop guitar_shop_test",
    "start": "node build/server/app.js",
    "deploy": "chmod +x ./bin/deploy.sh && ./bin/deploy.sh"
  ,
  "repository": 
    "type": "git",
    "url": "git+https://github.com/siddube/full-stack-app-server.git"
  ,
  "keywords": [
    "Image",
    "Resize"
  ],
  "author": "siddube",
  "license": "ISC",
  "bugs": 
    "url": "https://github.com/siddube/full-stack-app-server/issues"
  ,
  "homepage": "https://github.com/siddube/full-stack-app-server#readme",
  "devDependencies": 
    "@types/bcrypt": "^5.0.0",
    "@types/cors": "^2.8.12",
    "@types/express": "^4.17.12",
    "@types/jasmine": "^3.7.7",
    "@types/jsonwebtoken": "^8.5.4",
    "@types/node": "^16.0.0",
    "@types/pg": "^8.6.1",
    "@types/supertest": "^2.0.11",
    "@types/underscore": "^1.11.3",
    "@typescript-eslint/eslint-plugin": "^4.28.2",
    "@typescript-eslint/parser": "^4.28.2",
    "db-migrate-pg": "^1.2.2",
    "dotenv": "^10.0.0",
    "eslint": "^7.30.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^3.4.0",
    "jasmine-ts": "^0.4.0",
    "nodemon": "^2.0.13",
    "prettier": "^2.3.2",
    "ts-node": "^10.0.0",
    "typescript": "^4.3.5"
  ,
  "dependencies": 
    "bcrypt": "^5.0.1",
    "cors": "^2.8.5",
    "db-migrate": "^1.0.0-beta.16",
    "dotenv": "^10.0.0",
    "express": "^4.17.1",
    "jasmine": "^3.8.0",
    "jasmine-spec-reporter": "^7.0.0",
    "jsonwebtoken": "^8.5.1",
    "pg": "^8.7.1",
    "supertest": "^6.1.5",
    "underscore": "^1.13.1"
  ,
  "eslintIgnore": [
    "**/*[sS]pec.ts",
    "reporter.ts"
  ]

我认为主要问题是我无法运行在这些文件中定义的“npm run”脚本。当日志提示我检查可用的 npm run 命令时,所有这些命令都会显示出来。 'npm install' 工作正常。

非常感谢您提供解决此问题的帮助或建议。谢谢!

【问题讨论】:

【参考方案1】:

我很抱歉,但这是一个真正的 CircleCI 新手错误。我的客户端和服务器文件夹本身就是 git 存储库,根应用程序会在实例化管道时为它们创建空文件夹。通过创建两个新文件夹并在没有旧 git 存储库的情况下复制文件来解决它,它工作正常。

在我看来,这是我第一次实施 CI/CD 管道。

【讨论】:

以上是关于CircleCI 没有运行“npm run”命令的主要内容,如果未能解决你的问题,请参考以下文章

npm run build 打包后,如何运行在本地查看效果

我试图运行命令“npm run start”,它在 react js 中显示了这个错误

当你运行npm run命令时,会发生什么

使用 vue/cli 运行 npm run serve 时出现问题

无法使用 npm run develop 命令运行 Gatsby 应用程序

npm run dev 报错:missing script:dev