@Angular in global and non-local repository for all projects

Asked

Viewed 481 times

2

I installed @angular/cli like this:

npm install -g @angular/cli

I created a project like this:

ng new name-project

So far so good, but there is the possibility that when you create a project with eating ng new it does not copy the @Angula folder to location, but it uses the one from the global repository?

Reason for the question:

A clean design with only the angular has approximately 28,000 files with the total size of approximately 200 MB, to backup takes a lot, out it consumes hard disk space. I would like to unify the packages that are possible in the global repository and whenever I create my projects they will access the libraries there because all the projects will have the same versions of the libraries and if I update one all the projects will be updated.

===========================================================================

I ran the following lines on CMD simulating a project from scratch.

I created a folder Projects and inside it I made the commands:

mkdir node_modules
echo {} >> package.json
npm i @angular/cli --save
ng new projeto1 --skip-install

at the end presented the following message when I used ng new project1

"You cannot use the new command Inside an Angular CLI project."

1 answer

0

Yes, you can create a directory node_modules among projects.

Of node.js documentation

If the module Identifier passed to require() is not a core module, and does not Begin with '/', '../', or './', then Node.js Starts at the Parent directory of the Current module, and adds /node_modules, and Attempts to load the module from that Location. Node will not append node_modules to a path already ending in node_modules.

If it is not found there, then it moves to the Parent directory, and so on, until the root of the file system is reached.

For example, if the file at '/home/ry/projects/foo.js' called require('bar.js'), then Node.js would look in the following Locations, in this order:

  • /home/ry/projects/node_modules/bar.js
  • /home/ry/node_modules/bar.js
  • /home/node_modules/bar.js
  • /node_modules/bar.js

So basically what you should do is create a directory called node_modules at the root of your project directory, saving in this directory the common modules between your projects:

meus-projetos
│   package.json    
│
└───node_modules
│   │
│   └───@angular
│       │   ...
│   
└───meu-projeto-angular-01
│   │   tsconfig.json
│   │   ...
│   │
└───meu-projeto-angular-02
│   │   tsconfig.json
│   │   ...

Notice how there is only one file package.json that manages the shared directory node_modules.

Incidentally, using the command

npm install <nome-do-pacote> --save

will ensure automatic update of the dependencies section of that file.

  • If I use: "npm install jquery --save" inside my project folder, how will it include links/dependencies with modules that are in another package.json outside of my project folder? Note: I am using Visual Studio Code on Windows 10

  • @Joandreiycordeiro you must run the command inside the directory that contains the file package.json, and this file should manage the node_modules which is shared between projects. In the example I provided, you should run this command at the root of the directory meus-projetos

  • I did the following: I ran the following lines in CMD simulating a project from scratch. I created a Projects folder and inside it I did the commands: mkdir node_modules echo {} >> package.json npm i @angular/cli --save ng new project1 --Skip-install at the end presented the following message when I used ng new project1 "You cannot use the new command Inside an Angular CLI project."

Browser other questions tagged

You are not signed in. Login or sign up in order to post.