Ng new / Angular CLI
The standard (wrong) way:
Ok we are starting from scratch and we want to create an Angular App. Fortunately there is the handy angular cli that can help us to do so, and with the following two commands you are ready to code!
npm install -g @angular/cli
ng new my-new-app
Ok we've just created a new Angular app with a common global installed version of angular-cli , and now we can start to use all the ng commands available like ng start `ng build` But what happens if i have to create another project in the future? Or what happend if i have to checkout another old Angular project that have been created with a previous CLI version? Smell of mess here...
The right way:
You should't have installed in your global node_modules the angular-cli, this is necessary only to create the project with the `ng-new` command. Then you have to uninstall from your global modules, and add to the project package.json in the devDependencies node. In this way the angular cli with the right version is added to the project and we can start to use that instead of the global one!
npm i -D @angular/cli
So now you think that you could start again to do ng start but well the terminal is complaining. Of course! is not installed globally, in fact is installed in angular-project/.node_modules/.bin/ngok is quite annoying to do so and we have two solutions:
Use npm scripts
scripts: {
"start-server": "ng start"
}
and with npm run start-server npm is clever enough to search inside the project .node_modules/ before to check in the global one
Use npx
npx ng build
npx is a nice addition to npm command that will execute node modules added in the current project node_modules folder
That's it!