Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting an existing angular 2 project to use angular CLI

I have a project that was not created using ng new (rather, I followed the quickstart guide). When I run an angular CLI command in that project (such as ng generate) I get this error message:

You have to be inside an angular-cli project in order to use the generate command.

Is there a way I can turn my existing project into an angular-cli project? (I wanted to try using it because it apparently automatically works around an issue with barrel imports and SystemJS.)

like image 255
Coquelicot Avatar asked Aug 08 '16 18:08

Coquelicot


People also ask

How do I switch between Angular versions?

Open your command line in the directory containing your application's package. json folder and execute npm run ng serve. You can also experiment by running ng -v then npm run ng -v which will run the version of your global and local Angular-CLIs.

Can we run Angular without Angular CLI?

We can run the application in Visual Studio using F5 or Ctrl + F5 in Angular quick start application. Go to solution explore under src folder which has “index.

What is the Angular CLI version for Angular 7?

Install the Angular 7 CLI globally on your system by running the command npm i -g @angular/cli@7 . If you prefer to use the latest version of the cli you can run the command npm i -g @angular/cli . You can test that the Angular CLI was installed correctly by running the command ng version .


2 Answers

You should be able to run ng init and follow the prompts. Have a look at this issue if you need some guidance: https://github.com/angular/angular-cli/issues/755

Edit (March 13, 2017):

ng-init was removed from the latest version of the angular cli. https://github.com/angular/angular-cli/pull/4628

So for now you'll need to resort to mimicking what it tried to accomplish. ie:

  1. Create a new angular project ng new myTemplate

  2. Copy relevant files to your existing project from your new myTemplate project:

    .angular-cli.json package.json tslint.json src/polyfills.ts src/styles.css src/tsconfig.json

Depending on the state of your existing application you may want to copy over way more or way less. I guess this is why the feature was removed from the CLI. There isn't really any way for it to know exactly what you want it to do.

like image 81
Lucas Tétreault Avatar answered Oct 10 '22 15:10

Lucas Tétreault


ng init has been removed. What worked for me was to manually create a .angular-cli.json file in the root folder with the following keys:

 {     "$schema": "./node_modules/@angular/cli/lib/config/schema.json",     "project": {       "version": "1.0.0",       "name": "new-cli"     },     "apps": [       {         "root": "src",         "outDir": "dist",         "assets": [           "assets"         ],         "index": "index.html",         "main": "main.ts",         "polyfills": "polyfills.ts",          "prefix": "app",         "scripts": [],         "environmentSource": "environments/environment.ts"       }     ]   } 

You will have to change values according to your setup of course.

like image 39
Dilip Raj Baral Avatar answered Oct 10 '22 14:10

Dilip Raj Baral