I have a gulp based build system. Currently I have some tasks, e.g. a task that is accessing npm (require('npm')) programmatically. In order to achieve it I need to specify npm in my package.json dependencies, so that require can find it inside node_modules. However npm is obviously available along with node & I also have npm installed globally (latest 3.x version of npm).
Is there a way to require a global instance of npm? I do the same stuff e.g. with other npm packages (I mean I have global package installed but I duplicate it in package.json to make it available via require).
Yes you can do it by adding NODE_PATH to your environment variable eg:
export NODE_PATH=/usr/local/lib/node_modules/
After doing this you node should be able to find globally installed packages as well.
NOTE:your node module path may defer.
to make it permanent you can add the commmand to your ~/.bashrc file
Related
I am starting to learn React Native and I am very new to npm package manager. I read that npm can install packages localy or globaly but I am trying to understand what does that mean.
I am reading this page https://docs.npmjs.com/getting-started/installing-npm-packages-locally, can someone explain to me what does this mean please.
If you want to depend on the package from your own module using something like Node.js' require, then you want to install locally, which is npm install's default behavior. On the other hand, if you want to use it as a command line tool, something like the grunt CLI, then you want to install it globally.
Since I am very new to npm, React Native, Node (never used it), I am confused by the very first sentence in this quote. What does it mean "my own module?
If I want to use CRNA, I guess, I would have to install it globally?
If I am to install a package, say CRNA locally or globally, where do I see it installed on my MacBook Pro?
The difference between local and global install is that local install puts it into the node_modules directory of your project (this is what is referred to as "your own module") while global puts it into a system directory (the exact location depends on your OS, on OSX it should be /usr/local/lib/node_modules).
Basically:
Local install ties the installed module to your project: other projects on your computer do not get it but if your project is copied to another computer the module will be installed there too
global install ties it to your computer: you can use it on all of
your projects on your computer but if your project is copied to
another computer the installed module will not be there
And yes, CRNA should be installed globally as it is a general tool not a project's library dependence.
When you install package globally npm install -g <package name> modules drops in {prefix}/lib/node_modules.
Locally - npm install <package name> - drops package in the current working directory.
If you are going to require module in your project you have to install it locally.
If you want to run in from command line you need to install it globally.
If you need more extenden explanation take a look
Since I am very new to npm, React Native, Node (never used it), I am confused by the very first sentence in this quote. What does it mean "my own module?
If you have package.json file, then everything in the same folder is treated as "module". You add dependencies to it by doing npm install --save foo (--save option adds it under dependencies in your package.json).
If I want to use CRNA, I guess, I would have to install it globally?
Not sure what "CRNA" is. But general rule is that mostly everything (libraries...) are installed locally. Which means that they are added to your package.json and installed in same folder under node_modules.
Only case when you want to install something globally (can be added to package.json but is NOT installed in the same folder under node_modules but probably in your home directory), by doing npm install --global bar (--global installs it globally). Is when tool (not library) is project independent, as you can access it from everywhere. Something like create-react-app.
TLDR:
Local are dependencies (libraries) installed in same folder and (usually) added in your package.json as dependencie.
Global are tools installed in your user home folder and (usually) NOT added in your package.json as dependencie.
Let first start with how nodejs finds package.
Suppose you have some folder structure like-
root
-pixel
-project1
-project2
So, if your are working on project1 and required some npm package, nodejs tries to find a folder named node_modules in current directory. If fails, it goes parent(pixel folder) and tries to find node_modules and goes recursively upto root(which is global).
So, if there any package installed globally, you don't need to install it in your current working directory.
So, why don't we install all packages globally? Isn't it saves our harddisk memory?
Yes, true. But as npm packages are updating and changing its version everytime, its necessary to use specific package in your current working package to avoid collusion.
Then how global packages is useful?
Its good idea to install some cli packages to run directly from command line i.e webpack to easy our task.
What is the difference between installing a package locally and globally using npm?
From my understanding:
Locally install: npm install <package>
This package/module will find on your local node_modules folder and
can only be usable for this project.
This package/module can be accessible in using require("package")
from code.
This package/module can't be accessible in command line interface.
Globally install: npm install <package> -g
This package/module will find on where node is installed in your machine like /usr/local and can be usable everywhere.
This package/module can't be accessible in using require("package")
from code.
This package/module can be accessible in command line interface.
Please let me know. If I could misunderstand anything here. Thanks!
You are correct except for 1 point.
The local packages exposing CLI utilities can be accessed from the command line. Newer versions of NPM create this .bin/ directory inside the local node_modules/.
Whenever you try to use a tool (let's take babel for example), if you use it from the command line and you have it installed in your project, npm will properly identify that package and run it's CLI for you.
Here's a useful article on the topic.
http://www.2ality.com/2016/01/locally-installed-npm-executables.html
Global modules are mostly tools like gulp, yoman or any other module you use in your daily work.
Local modules are the dependencies of your project. You should never depend on a global module in your project. Even dependencies as gulp should be a local dependency in your dev-dependency section.
I have globally installed packages like bower, jshint, etc. On running npm init, it does not include dependencies. Is there a way where dependencies would be pre-included in package.json using globally installed packages on running npm init.
No. If your project needs explicit dependencies, they must be added to package.json manually or with npm install --save
npm init will creats a new package.json file based on your configuration that you specify. However you can use globally installed packages without specify them in package.json. But if you want to migrate your code from one system to another, then you have to manually specify all globally installed packages with name & version in package.json and after that you can use npm install for that.
Global dependencies are not supposed to be your new project's dependencies. Global dependencies are supposed to be command line tools such as pm2, forever, mocha, Istanbul etc. Package.json and global dependencies serve totally different purpose.
If you still find the need of doing so please explain exact package you need both at global and project.
In my package.json file, I have bower listed as a dependency. After I run npm install, bower gets installed locally. When I try to run bower after installing it locally I get an error
"bower" is not recognized as an internal or external command
It seems the only way to resolve this is to install bower globally. Why should I have to do this? If my project contains a local copy of bower, why won't node use it?
Installing locally makes bower available to the current project (where it stores all of the node modules in node_modules). This is usually only good for using a module like so var module = require('module'); It will not be available as a command that the shell can resolve until you install it globally npm install -g module where npm will install it in a place where your path variable will resolve this command.
Edit: This documentation explains it pretty thorougly.
You can execute your local instance by typing the line below in cmd:
node_modules/bower/bin/bower <bower args>
We use both PHP and JavaScript, so we have composer and npm.
Each of the projects we work on have different packages both for runtime of the package as well as build/dev tools.
As there are version constraints in each project, installing version x of a package globally (that would be run from the command line), would cause us issues, we install all the tooling in each package. Much easier to define in the appropriate composer.json / package.json files.
But running the CLI tools is a pain if you have to constantly add an additional path to the command.
To that end, we have recommend to the team that the following paths are added to your $PATH in the appropriate .bashrc (or equivalent):
./vendor/bin:./node_modules/.bin
(EDIT: For Windows, the paths would be .\vendor\bin;.\node_modules\.bin;)
So, whilst in project X, we have access to the CLI tools for that project. Switch to project Y, and we get that projects tools.
Sure, you are going to get duplications, but each project is maintained by different teams (and some people are in multiple teams), so again, having 1 version in the global setup is an issue there.
Usually you install NPM modules globally if you want them included in your path to be ran from the command line. Since it is installed locally you will have to run it from the node_modules folder.
I have installed node.js at my local system (path : C:\Program Files\nodejs). I installed some modules and expected them to be placed in (C:\Program Files\nodejs\node_modules). But, the installed modules are placed at C:\Users\Administrator\AppData\Roaming\npm\node_modules. Is this right or wrong? Can i access these modules globally?
My system platform is windows 7, and my version of node is 0.10.
The current (January 2018) version of Node.js is 9.4.0, so I'm not sure if it is compatible with your version.
You can set the default global installation path for node_modules by modifying your npmrc file.
Execute in a prompt: npm config list. It should among other things display a prefix setting, which is set to your Roaming AppData folder, for example: C:\Users\Administrator\AppData\Roaming\npm. You can override this setting by executing npm config set prefix C:\Program Files\nodejs\node_modules\npm.
Now, once you install node_modules globally they will be placed in that directory.
Running just npm install will install all modules into a folder in the current directory called node_modules; all files within the same root directory (even in sibling folders), will check for modules here when you call require. You should install any modules that you wish to require in your project this way.
If you want to install a module from npm globally, you can append the -g flag. This is usually for command-line tools, which you want to be accessible across multiple projects. An example would be npm install nodemon -g
If you are still confused, I recommend you reference this blog post from the makers of node on global/local installation.
You can find out default paths (user's and global's ones) by command:
npm config list
It is in 'prefix' variable, e.g.:
; userconfig C:\Users\pavel\.npmrc
cache = "C:\\ProgramData\\npm-cache"
prefix = "C:\\ProgramData\\npm"
To change default path have to use the command, e.g.:
npm config set prefix="C:\ProgramData\npm"
By default, any packages you install will install to a global installation directory which is why they are showing up in C:\Users\Administrator\AppData\Roaming\npm\node_modules. If you want to install the packages to your local node_modules folder you will need to type in the following:
npm install (package name) --save-dev