What’s the difference of putting -g in package installations by npm, webpack, etc...?

Asked

Viewed 691 times

2

It’s been a while since I installed packages via prompt with npm, Bower among other package managers, but I never really understood what the command is for -g. I know it means global, installing the packages globally on the computer and not just locally on the project specifically. But my doubts are:

1 - With the use of -g where installed packages are located on the computer?

2 - For example, install Vue.js like this npm install -g @vue-cli in one project, and in another project using Vue.js install it with the command npm install @vue-cli, where Vue is found, from the internet repository or from my computer?

3 - If you install Vue.js in a project globally with a particular version and happen to install it again globally with a newer version in another project, the first project that is using the old version may cause problems, the last one overwrites the previous one or two Vue.js are allocated?

1 answer

1


The main difference between installing a package globally is that you start executing the commands on the command line, according to the [doc of npm])https://docs.npmjs.com/getting-started/installing-npm-packages-globally#how-to-install-global-Packages):

There are two Ways to install npm Packages: locally or globally. Choose which Kind of installation to use based on how you want to use the package.

If you want to use a package as a command line tool, then install it globally. This way, it Works no Matter which directory is Current. This is the Choice you would use if you Were Installing Grunt, for example.

If you want to Depend on the package from your Own module, then install it locally. This is the Choice you would use if you are using require statements, for example.

Translated like this:

There are two ways to install packages: locally or globally. Choose which installation type based on how you want to use the package.

If you want to use the package as a command line tool, then install the command globally. This way, the command works regardless of which directory you are in. This is the choice that you user if installing Grunt, for example.

If you want to depend on the package in its own module, then install it locally. This is the choice you would use if using require, for example.

  1. It depends on the operating system and configuration performed, but usually the packages are located inside the user folder.

    • Macos: /usr/local/lib/node_modules
    • Windows: Appdata Roaming npm
  2. I don’t quite understand the question de onde é buscado. Both commands npm i with or without -g will download the latest stable package published on NPM, remembering that the version of the package that will be downloaded may be another depending on your file package.json. Documentation

  3. If you install the vue globally will make no difference as this package needs to be installed locally (unless you perform a npm link because then he will pick up the package in the global). Documentation

    • No problem you update Vue-cli as it is a development tool.
    • When you talk about installing Vue.js globally, you say install the Vue-cli package, right? Cli packages are usually not installed locally because it is a tool to use via command line, according to the documentation theirs:

      The CLI (@Vue/cli) is a globally installed npm package and provides the Vue command in your terminal. It provides the Ability to quickly scaffold a new project via Vue create, or Instantly prototype new Ideas via Vue serve. You can also Manage your Projects using a Graphical user interface via Vue ui. We will walk through what it can do in the next few sections of the guide.

  • Thanks for helping out, man. As your answer to the second question, then pq install globally, if in another project it downloads again from NPM, why then install globally? I didn’t understand the difference between the two facilities.

  • 1

    When we executed the npm i in another project, npm will download the packages defined in the configuration file packages.json. I translated the first citation of the NPM document. See if it gets clearer.

Browser other questions tagged

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