Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript npm Basics (retiring) Updating and Uninstalling Packages with npm Updating Packages with npm

In witch way updating a global package will affect a local package?

These questions may seem silly but the teacher explains that we should be careful updating global packages. I understand it will affect new projects and already existing projects without a package.json file. But how can it affect an already existing project with a package.json file? So imagine, I have jQuery package installed globally and I also have a project A with jQuery incorporated locally as a dependency in package.json.

I have two questions:

1) ins't the local package a kind of a different package because it has its own rules in package.json? So, OK, it still is the global package but controlled with restrictions. In which way changing jQuery globally to the last version will change a jQuery package already installed locally that have rules set by package.json that don't allow an update to a major version(for instance)?

2) If this happens, if changing global packages can affect already existing projects with package.json rules, does a change in the global npm package can affect dramatically already existing projects? Because we are talking about the package that manages all the packages:) Shouldn't we be REALLY CAREFUL updating npm whenever it goes up a major version?

1 Answer

Neil McPartlin
Neil McPartlin
14,660 Points

Hi Carlos. I've just covered this course and actually used NPM in a project, so although you posed these great questions 3 months ago, I'd like to take a stab at them.

ins't the local package a kind of a different package because it has its own rules in package.json?

Yes. The entire (local) installation resides within a single umbrella folder so packages are not intended to be accessed from outside.

So, OK, it still is the global package but controlled with restrictions

No. Think of them as 2 separate installations. Each are controlled by their own package.json file.

In which way changing jQuery globally to the last version will change a jQuery package already installed locally that have rules set by package.json that don't allow an update to a major version(for instance)?

The package.json responsible for the local installation solely dictates what will happen.

If this happens, if changing global packages can affect already existing projects with package.json rules, does a change in the global npm package can affect dramatically already existing projects?

As explained above, for as long as an app relies solely on the local installation of a given package, any change made to the global installation will have no effect. So are there times when one would want an app to rely on a globally installed package? Well apparently there are. More about that below**.

Shouldn't we be REALLY CAREFUL updating npm whenever it goes up a major version?

Absolutely. Local or global, a major version will bring many benefits no doubt but some new features will not be backward compatible, meaning for example that older browsers may no longer work reliably with your app.

So now I imagine you are left wondering when your app should ever rely on a globally installed package? Well you are not alone. If you google, you will find many feeling the same way. Here is a blog by 'Bart' who used to work on NPM support...

http://www.bartread.com/2014/02/17/whats-difference-locally-globally-installing-npm-packages/

To quote his 'takeaways'...

Global packages are for anything that you need to access from the shell. By contrast local packages are for using within your apps.

always try to use local packages unless there’s a very good reason not to! (I.e., you need to use the package from the command line.)

**Bart also gives an example of why you might want to associate multiple apps to a single globally installed package called 'Express', in order to use its 'Command Line' functionality. He explains how this can be done by using a feature called 'link'. The advantage here being that when you want to update 'Express' you just do it globally once, and all the apps get the benefit.

I'm a bit late to the party but I had the same question, and just wanted to say thanks for such an awesome answer! Definitely helped me get my head around the concept better.