0

I followed/clone this github repo:

I did the: npm install

Then I did the: npm start

Project can run.

Then I want to add a new component to this project. I use: ng generate component my-foo-component

But I got this error:

You have to be inside an Angular CLI project in order to use the generate command

I found some posts in stackoverflow related to my question, I tried, but with no luck. How to solve my problem. It seems like this project is not created by angular cli, so I cannot use command like: ng generate ...

I checked my package.json, I can see the angular cli as dependency?

My package.json:

{
  "name": "angular-starter",
  "version": "6.0.0",
  "description": "An Angular Webpack Starter kit featuring Angular (Router, Http, Forms, Services, Tests, E2E, Coverage), Karma, Protractor, Jasmine, Istanbul, TypeScript, and Webpack by AngularClass",
  "keywords": [
    "angular",
    "angular2",
    "angular4",
    "webpack",
    "typescript"
  ],
  "author": "Patrick Stapleton <[email protected]>",
  "homepage": "https://github.com/AngularClass/angular-starter",
  "license": "MIT",
  "scripts": {
    "build:aot:prod": "npm run clean:dist && npm run clean:aot && cross-env BUILD_AOT=1 npm run webpack -- --config config/webpack.prod.js  --progress --profile --bail",
    "build:aot": "npm run build:aot:prod",
    "build:dev": "npm run clean:dist && npm run webpack -- --config config/webpack.dev.js --progress --profile",
    "build:docker": "npm run build:prod && docker build -t angular2-webpack-start:latest .",
    "build:prod": "npm run clean:dist && npm run webpack -- --config config/webpack.prod.js  --progress --profile --bail",
    "build": "npm run build:dev",
    "ci:aot": "npm run lint && npm run test && npm run build:aot && npm run e2e",
    "ci:jit": "npm run lint && npm run test && npm run build:prod && npm run e2e",
    "ci:nobuild": "npm run lint && npm test && npm run e2e",
    "ci:testall": "npm run lint && npm run test && npm run build:prod && npm run e2e && npm run build:aot && npm run e2e",
    "ci:travis": "npm run lint && npm run test && npm run build:aot && npm run e2e:travis",
    "ci": "npm run ci:testall",
    "clean:dll": "npm run rimraf -- dll",
    "clean:aot": "npm run rimraf -- compiled",
    "clean:dist": "npm run rimraf -- dist",
    "clean:install": "npm set progress=false && npm install",
    "clean": "npm cache clean --force && npm run rimraf -- node_modules doc coverage dist compiled dll",
    "docker": "docker",
    "docs": "npm run typedoc -- --options typedoc.json --exclude '**/*.spec.ts' ./src/",
    "e2e:live": "npm-run-all -p -r server:prod:ci protractor:live",
    "e2e:travis": "npm-run-all -p -r server:prod:ci protractor:delay",
    "e2e": "npm-run-all -p -r server:prod:ci protractor",
    "github-deploy:dev": "npm run webpack -- --config config/webpack.github-deploy.js --progress --profile --env.githubDev",
    "github-deploy:prod": "npm run webpack -- --config config/webpack.github-deploy.js --progress --profile --env.githubProd",
    "github-deploy": "npm run github-deploy:dev",
    "lint": "npm run tslint \"src/**/*.ts\"",
    "node": "node",
    "postinstall": "npm run webdriver:update",
    "postversion": "git push && git push --tags",
    "preclean:install": "npm run clean",
    "preversion": "npm test",
    "protractor": "protractor",
    "protractor:delay": "sleep 3 && npm run protractor",
    "protractor:live": "protractor --elementExplorer",
    "rimraf": "rimraf",
    "server:dev:hmr": "npm run server:dev -- --inline --hot",
    "server:dev": "npm run webpack-dev-server -- --config config/webpack.dev.js --open --progress --profile --watch --content-base src/",
    "server:prod": "http-server dist -c-1 --cors",
    "server:prod:ci": "http-server dist -p 3000 -c-1 --cors",
    "server": "npm run server:dev",
    "start:hmr": "npm run server:dev:hmr",
    "start": "npm run server:dev",
    "test": "npm run lint && karma start",
    "tslint": "tslint",
    "typedoc": "typedoc",
    "version": "npm run build",
    "watch:dev:hmr": "npm run watch:dev -- --hot",
    "watch:dev": "npm run build:dev -- --watch",
    "watch:prod": "npm run build:prod -- --watch",
    "watch:test": "npm run test -- --auto-watch --no-single-run",
    "watch": "npm run watch:dev",
    "webdriver-manager": "webdriver-manager",
    "webdriver:start": "npm run webdriver-manager start",
    "webdriver:update": "webdriver-manager update",
    "webpack-dev-server": "node --max_old_space_size=4096 node_modules/webpack-dev-server/bin/webpack-dev-server.js",
    "webpack": "node --max_old_space_size=4096 node_modules/webpack/bin/webpack.js"
  },
  "dependencies": {
    "@angular/animations": "~4.1.2",
    "@angular/common": "~4.1.2",
    "@angular/compiler": "~4.1.2",
    "@angular/core": "~4.1.2",
    "@angular/forms": "~4.1.2",
    "@angular/http": "~4.1.2",
    "@angular/platform-browser": "~4.1.2",
    "@angular/platform-browser-dynamic": "~4.1.2",
    "@angular/platform-server": "~4.1.2",
    "@angular/router": "~4.1.2",
    "@angularclass/hmr": "~1.2.2",
    "@angularclass/hmr-loader": "~3.0.2",
    "core-js": "^2.4.1",
    "http-server": "^0.9.0",
    "ie-shim": "^0.1.0",
    "reflect-metadata": "^0.1.10",
    "rxjs": "~5.0.2",
    "zone.js": "0.8.5"
  },
  "devDependencies": {
    "@angular/compiler-cli": "~4.0.3",
    "@types/hammerjs": "^2.0.34",
    "@types/jasmine": "2.5.45",
    "@types/node": "^7.0.13",
    "@types/source-map": "^0.5.0",
    "@types/uglify-js": "^2.6.28",
    "@types/webpack": "^2.2.15",
    "add-asset-html-webpack-plugin": "^1.0.2",
    "angular2-template-loader": "^0.6.2",
    "assets-webpack-plugin": "^3.5.1",
    "awesome-typescript-loader": "~3.1.2",
    "codelyzer": "~2.1.1",
    "copy-webpack-plugin": "^4.0.1",
    "css-loader": "^0.28.0",
    "cross-env": "^5.0.0",
    "exports-loader": "^0.6.4",
    "expose-loader": "^0.7.3",
    "extract-text-webpack-plugin": "~2.1.0",
    "file-loader": "^0.11.1",
    "find-root": "^1.0.0",
    "gh-pages": "^1.0.0",
    "html-webpack-plugin": "^2.28.0",
    "imports-loader": "^0.7.1",
    "inline-manifest-webpack-plugin": "^3.0.1",
    "istanbul-instrumenter-loader": "2.0.0",
    "jasmine-core": "^2.5.2",
    "json-loader": "^0.5.4",
    "karma": "^1.6.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-coverage": "^1.1.1",
    "karma-jasmine": "^1.1.0",
    "karma-mocha-reporter": "^2.2.3",
    "karma-remap-coverage": "^0.1.4",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-webpack": "~2.0.3",
    "ng-router-loader": "^2.1.0",
    "ngc-webpack": "~2.0.0",
    "node-sass": "^4.5.2",
    "npm-run-all": "^4.0.2",
    "optimize-js-plugin": "0.0.4",
    "parse5": "^3.0.2",
    "preload-webpack-plugin": "^1.2.2",
    "protractor": "^5.1.1",
    "raw-loader": "0.5.1",
    "rimraf": "~2.6.1",
    "sass-loader": "^6.0.3",
    "script-ext-html-webpack-plugin": "^1.7.1",
    "source-map-loader": "^0.2.1",
    "string-replace-loader": "~1.2.0",
    "style-loader": "^0.18.1",
    "to-string-loader": "^1.1.5",
    "ts-node": "^3.0.2",
    "tslib": "^1.6.1",
    "tslint": "~4.5.1",
    "tslint-loader": "^3.5.2",
    "typedoc": "^0.7.1",
    "typescript": "~2.2.2",
    "url-loader": "^0.5.8",
    "webpack": "~2.6.1",
    "webpack-dev-middleware": "^1.10.1",
    "webpack-dev-server": "~2.4.2",
    "webpack-dll-bundles-plugin": "^1.0.0-beta.5",
    "webpack-merge": "~4.1.0"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/AngularClass/angular-starter.git"
  },
  "bugs": {
    "url": "https://github.com/AngularClass/angular-starter/issues"
  },
  "engines": {
    "node": ">= 4.2.1",
    "npm": ">= 3"
  }
}
3
  • Move inside the project Commented Jul 16, 2017 at 3:44
  • @alehn96 Do you mean I create a new angular cli project, then move my current code inside this new angular cli project? But I want to fix it in my current project without creating a new angualar cli project. Commented Jul 16, 2017 at 3:52
  • If the project not is CLI, you cannot use cli generators Commented Jul 16, 2017 at 4:13

3 Answers 3

1

if you want to use angular-cli only for generations like

  • ng g component my-new-component
  • ng g directive my-new-directive
  • ng g pipe my-new-pipe
  • ng g service my-new-service
  • ng g class my-new-class
  • ng g guard my-new-guard
  • ng g interface my-new-interface
  • ng g enum my-new-enum
  • ng g module my-module

and not actions like

  • ng build
  • ng serve
  • etc...

you can do following:

  1. be sure to be at the root of your app (where node_modules folder located)
  2. run npm install @angular/cli, run it even if you have angular cli installed globally, you need this entry in your package.json file
  3. create a file called .angular-cli.json with very minimal settings like:

    {
      "$schema": "./node_modules/@angular/cli/lib/config/schema.json",   
      "project": {
        "name": "angular-starter"   
      },   
      "apps": [
        {
          "root": "src",
          "index": "index.html",
          "main": "main.ts"
        }   
      ],   
      "defaults": {
        "styleExt": "css"   
      } 
    }
    

At this point you should be able to run

ng c my-new-component

in case you do not have ng command installed globally, you can run it as

node_modules/.bin/ng c my-new-component

make sure to explore all the available for generation options:

Sign up to request clarification or add additional context in comments.

2 Comments

Your solution works. May I know where can I find the reference document about your step 2 and step 3?
I figured out these steps by examining error messages: 'You have to be inside an Angular CLI project in order to use the generate command' is caused because there is no 'angular-cli' dependency in your package.json, and another error 'Unable to find any apps in .angular-cli.json.' points to missing file which blueprint can be copied from github.com/angular/angular-cli/blob/master/packages/%40angular/…
1

For to Generate Component you have to go inside project folder if you are using Visual Studio Code then

 Go to 
1-View
2-Integrated Terminal
or ( CTRL+` )
use the Integrated Terminal for to generate component

ng generate component my-foo-component (or)
ng g component my-foo-component

if you are using other Editor then use your command Prompt

Note:_
in command prompt you most be inside project folder

1 Comment

I am pretty sure that I was under a sub folder to do the 'ng generate ...', but still have that error. Will you be able to download the project and try?
0

For generating component, services, or pipe inside of project you have to be inside of desired folder. For example, if you want to generate a pipe for a user component then you have to be inside of user folder.

In term of generating component use :

cd project
cd src/app

then

ng generate component my-foo-component

3 Comments

I am pretty sure that I was under a sub folder to do the 'ng generate ...', but still have that error. Will you be able to download the project and try?
Yea, I can try on your project
I fixed the problem after I tried the accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.