Unexpected NodeJS and NPM behavior - javascript

Does npm install only install packages in the directory you run it from? Because that's my current experience with it. At first, I ran npm install xml in a command prompt at C:/Users/ME. Running require("xml"); in a node instance that was run from C:/Users/ME works, and running npm ls lists the xml package...
But if I move to any other directory, neither of them do.
Is this the expected behavior (doesn't sound right), a Windows thing, or am I missing some kind of install option?

Yes, this is normal behavior.
npm install (in package directory, no arguments):
Install the dependencies in the local node_modules folder.
In global mode (ie, with -g or --global appended to the command), it
installs the current package context (ie, the current working
directory) as a global package.
By default, npm install will install all modules listed as
dependencies. With the --production flag (or when the NODE_ENV
environment variable is set to production), npm will not install
modules listed in devDependencies.
NPM Install Docs

npm installs modules either below the current directory (in a node_modules sub-directory) or if you use the -g flag when running it, it install modules in the global location which is determined via OS and configuration and then node.js will be able to find the module no matter where it is being loaded from.
So, this will install a module globally:
npm install -g xml
When using require(), if you want require to look in the current directory to load a module, then you have to do it like require("./module");, otherwise it will not look for the modules install in the current directory.

You need to do npm install -g xml. This would install the packages globally.
'-g' represents global. Then when you check on other directories, you could list the same package

Note the way you install node is important as well!
You can install node with:
chocolatey (https://chocolatey.org/packages?q=nodejs)
nodeJS website (https://nodejs.org/)
*Knowing where to set your user variables is essential know how for windows developers! The reason I mention this is that the list of chocolatey, and node USER : PATH variables gets so large at one point nodejs and with it npm will just stop working. You need to work within your env. variables in one scenario where that happens. (Can typically happen with multiple versions of SQL Server installed)
If you want an excellent free, offline-available resource on node and npm I suggest devdocs (http://www.devdocs.io) and as well GentlNode's cheat sheets (https://gentlenode.com/journal/cheatsheet) *GentleNode not avail. offline FYI.
There's some key information you haven't received yet. See below:
npm install xml - installs "xml" package and required modules to the local folder you are in when you run the command. Files exist for you to work on in this folder alone(and thus commands only work in this folder) and "xml" cannot be called in cmd/terminal outside of this folder(without rerunning with "-g" param) "xml" is NOT written to package.json file. When and if project is uploaded to github will upload in full if you don't add node_modules to your .gitignore file.
npm install -g xml - install "xml" to your global npm folder, which is referenced to your "user environment variables" (what allows you to call specific functions, methods, and executables from within the command line) Files are NOT installed to your project folder, not recorded in "package.json" file. Will NOT upload anything to github.
npm install --save xml - using "--save" allows you to "install" to this folder, but also to write the package you've included into your project's "package.json" file. Here, your aim is to distribute your software and you want as little files on github/source control as possible that npm knows how to install like jQuery, XML, body-parser, etc. etc.
You would typically use a regular "npm install XML" command for a lightweight, rapid release mini-tool to support a key feature or process improvement you just want to keep on your local machine. In addition, you could npm install "packages" to a folder, and use that as a backup "node folder" mapped to your environment variables.
You would typically use a global "npm install -g XML" command for something like middleware, express, node updates installed VIA npm, and other processes you wish to have access to in cmd/terminal from "anywhere." Once this is established for your core development packages in node, you often just use the proceeding command, read the explanation for more detail.
Lastly, you would use "npm install --save" to write into the "package.json" file the required module(s) you've installed to your project. This would be for those who have worked heavily in node and have used "npm install -g" to already globally install their most common core nodeJS. This will write to the package.json the packages and versions being leveraged in your software currently so that your end-user can download the lightweight version and use npm package management to install all of the module dependencies so the app works correctly on other people's computers.
*Note, this leads to the last command "npm install" which you can run in a given folder where you've downloaded the file directly from github.
Hope that helps a bit more for the given facets of installation using npm!

Related

What's the difference between npm install and manual installation?

So i'm looking at requirejs. I can either install this package with npm install requirejs, or download it manually from the website. What's the difference? Are there tradeoffs to either one? Is npm install just a fancier way of manually installing? Thanks.
imagine you have 10 libraries in package.json, and you want to install all of them at once, you can just do "npm i" and it will take care of all, in just ~10sec. compare this with installing those 10 lib manually, it is indeed a good friend, to help you ease the process of downloading lib for you in no time.
Yes, npm install is just a fancier way to add package that help saving your precious time.
Base on the description here: npm install.
If the package has a package-lock, or an npm shrinkwrap file, or a yarn lock file, the installation of dependencies will be driven by that
Install the dependencies in the local node_modules folder.
In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package.
By default, npm install will install all modules listed as dependencies in package.json.
With the --production flag (or when the NODE_ENV environment variable is set to production), npm will not install modules listed in devDependencies. To install all modules listed in both dependencies and devDependencies when NODE_ENV environment variable is set to production, you can use --production=false.
Without a package manager like npm or yarn, it could take you a lot of time just for installing stuff. And while you are developing with node js, you'd have ton of things to install.
Every modern programming language has it's own package manager, so why you ever need to manually install them?

Difference between npx and npm?

I have just started learning React, and Facebook helps in simplifying the initial setup by providing the following ready-made project.
If I have to install the skeleton project I have to type npx create-react-app my-app in command-line.
I was wondering why does the Facebook in Github have npx create-react-app my-app rather than npm create-react-app my-app?
Introducing npx: an npm package runner
NPM - Manages packages but doesn't make life easy executing any.NPX - A tool for executing Node packages.
NPX comes bundled with NPM version 5.2+
NPM by itself does not simply run any package. It doesn't run any package as a matter of fact. If you want to run a package using NPM, you must specify that package in your package.json file.
When executables are installed via NPM packages, NPM links to them:
local installs have "links" created at ./node_modules/.bin/ directory.
global installs have "links" created from the global bin/ directory (e.g. /usr/local/bin) on Linux or at %AppData%/npm on Windows.
Documentation you should read
NPM:
One might install a package locally on a certain project:
npm install some-package
Now let's say you want NodeJS to execute that package from the command line:
$ some-package
The above will fail. Only globally installed packages can be executed by typing their name only.
To fix this, and have it run, you must type the local path:
$ ./node_modules/.bin/some-package
You can technically run a locally installed package by editing your packages.json file and adding that package in the scripts section:
{
"name": "whatever",
"version": "1.0.0",
"scripts": {
"some-package": "some-package"
}
}
Then run the script using npm run-script (or npm run):
npm run some-package
NPX:
npx will check whether <command> exists in $PATH, or in the local project binaries, and execute it. So, for the above example, if you wish to execute the locally-installed package some-package all you need to do is type:
npx some-package
Another major advantage of npx is the ability to execute a package which wasn't previously installed:
$ npx create-react-app my-app
The above example will generate a react app boilerplate within the path the command had run in, and ensures that you always use the latest version of a generator or build tool without having to upgrade each time you’re about to use it.
Use-Case Example:
npx command may be helpful in the script section of a package.json file,
when it is unwanted to define a dependency which might not be commonly used or any other reason:
"scripts": {
"start": "npx gulp#3.9.1",
"serve": "npx http-server"
}
Call with: npm run serve
Related questions:
How to use package installed locally in node_modules?
NPM: how to source ./node_modules/.bin folder?
How do you run a js file using npm scripts?
npx is a npm package runner (x probably stands for eXecute). One common way to use npx is to download and run a package temporarily or for trials.
create-react-app is an npm package that is expected to be run only once in a project's lifecycle. Hence, it is preferred to use npx to install and run it in a single step.
As mentioned in the main page https://www.npmjs.com/package/npx, npx can run commands in the PATH or from node_modules/.bin by default.
Note:
With some digging, we can find that create-react-app points to a Javascript file (possibly to /usr/lib/node_modules/create-react-app/index.js on Linux systems) that is executed within the node environment. This is simply a global tool that does some checks. The actual setup is done by react-scripts, whose latest version is installed in the project. Refer https://github.com/facebook/create-react-app for more info.
NPM is a package manager, you can install node.js packages using NPM
NPX is a tool to execute node.js packages.
It doesn't matter whether you installed that package globally or locally. NPX will temporarily install it and run it. NPM also can run packages if you configure a package.json file and include it in the script section.
So remember this, if you want to check/run a node package quickly without installing locally or globally use NPX.
npM - Manager
npX - Execute - easy to remember
npm - Package manager for JavaScript, just like: pip (Python), Maven (Java), NuGet (.NET), Composer (PHP), RubyGems (Ruby), ...
npx - runs a command of a package without installing it explicitly.
Use cases:
You don't want to install packages neither globally nor locally.
You don't have permission to install it globally.
Just want to test some commands.
Sometime, you want to have a script command (generate, convert something, ...) in package.json to execute something without installing these packages as project's dependencies.
Syntax:
npx [options] [-p|--package <package>] <command> [command-arg]...
Package is optional:
npx -p uglify-js uglifyjs --output app.min.js app.js common.js
+----------------+ +--------------------------------------------+
package (optional) command, followed by arguments
For example:
Start a HTTP Server : npx http-server
Lint code : npx eslint ./src
# Run uglifyjs command in the package uglify-js
Minify JS : npx -p uglify-js uglifyjs -o app.min.js app.js common.js
Minify CSS : npx clean-css-cli -o style.min.css css/bootstrap.css style.css
Minify HTML : npx html-minifier index-2.html -o index.html --remove-comments --collapse-whitespace
Scan for open ports : npx evilscan 192.168.1.10 --port=10-9999
Cast video to Chromecast : npx castnow http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4
More about command:
https://docs.npmjs.com/files/package.json#bin
https://github.com/mishoo/UglifyJS2/blob/master/package.json#L17
NPX:
From https://www.futurehosting.com/blog/npx-makes-life-easier-for-node-developers-plus-node-vulnerability-news/:
Web developers can have dozens of projects on their development
machines, and each project has its own particular set of npm-installed
dependencies. A few years back, the usual advice for dealing with CLI
applications like Grunt or Gulp was to install them locally in each
project and also globally so they could easily be run from the command
line.
But installing globally caused as many problems as it solved. Projects
may depend on different versions of command line tools, and polluting
the operating system with lots of development-specific CLI tools isn’t
great either. Today, most developers prefer to install tools locally
and leave it at that.
Local versions of tools allow developers to pull projects from GitHub
without worrying about incompatibilities with globally installed
versions of tools. NPM can just install local versions and you’re good
to go. But project specific installations aren’t without their
problems: how do you run the right version of the tool without
specifying its exact location in the project or playing around with
aliases?
That’s the problem npx solves. A new tool included in NPM 5.2, npx is
a small utility that’s smart enough to run the right application when
it’s called from within a project.
If you wanted to run the project-local version of mocha, for example,
you can run npx mocha inside the project and it will do what you
expect.
A useful side benefit of npx is that it will automatically install npm
packages that aren’t already installed. So, as the tool’s creator Kat
Marchán points out, you can run npx benny-hill without having to deal
with Benny Hill polluting the global environment.
If you want to take npx for a spin, update to the most recent version
of npm.
Simple Definition:
npm - Javascript package manager
npx - Execute npm package binaries
Here's an example of NPX in action: npx cowsay hello
If you type that into your bash terminal you'll see the result. The benefit of this is that npx has temporarily installed cowsay. There is no package pollution since cowsay is not permanently installed. This is great for one off packages where you want to avoid package pollution.
As mentioned in other answers, npx is also very useful in cases where (with npm) the package needs to be installed then configured before running. E.g. instead of using npm to install and then configure the json.package file and then call the configured run command just use npx instead. A real example:
npx create-react-app my-app
NPM => Is a JS package manager.
NPX => Is a tool for executing Node packages and execute npm package binaries.
It is easy to remember:
-npm stands for MANAGER
-npx stands for EXECUTE
NPM: NPM stands for Node Package Manager and is the default package manager for Node.js. It was developed by Isaac Z. Schlueter and was originally released on January 12, 2010. It is entirely written in JavaScript. It consists of a command-line client npm which manages all node.js packages and modules. When node.js is installed, it is included in the installation.
npm run your-package-name
NPX is a tool that use to execute packages.
NPX is an acronym for Node Package Execute The NPX package comes with npm, so when you install npm above 5.2.0, NPX will be installed automatically.
It is an npm package runner that can execute any package that you want from the npm registry without even installing that package. The npx is useful during a single time use package. If you have installed npm below 5.2.0 then npx is not installed in your system.
Run the following command to determine if npx is installed:
npx -v
The following command can be run if npx is not installed.
npm install -g npx
Use npx to execute the package:
npx your-package-name
Simplest Definition:
NPX
The npx stands for Node Package Execute and it comes with the npm,
when you installed npm above 5.2.0 version then automatically npx will
installed. It is an npm package runner that can execute any package
that you want from the npm registry without even installing that
package.
NPM
npm is a package manager for the JavaScript programming language
maintained by npm, Inc. npm is the default package manager for the
JavaScript runtime environment Node.js. It consists of a command line
client, also called npm, and an online database of public and paid-for
private packages
NPM vs. NPX
NPM stands for the Node Package Manager. A text based program for Nodejs package management.
While NPX is a Node Package Runner. Its function is to execute the Nodejs package
NPX will execute binary files from the Nodejs package, both installed and not.
Even NPX can also help us use certain versions of Nodejs without having to use nvm (node.js version management), nave (node.js virtual environment), and nvm (node.js version management).
NPM stands for Node Package Manager. NPM is Node.JS's default package manager. It's written in Javascript. The role of NPM is to manage the package and modules of node.js.
NPX stands for Node Package Execute. NPX comes with npm, when npm is installed above the 5.2.0 version, it gets installed automatically. NPX is an npm package runner and its role is to execute the package from the registry without even installing that package.
Now, the differences between NPM and NPX are as below:
i) NPM is used to install the packages while NPX is used to execute the packages.
ii) Due to npm the packages installed have to be taken care of since it's installed globally while the packages which are used by npx don't need to be taken care of as they are not installed globally.
Simple answer is like
NPX: is used to execute any node package without installing the package on our machine.
NPM: is used to install any node js package in our machine. We can use "require("package-name')" when we install any package using NPM. but we can not import the package when we use NPX.
Example: You should run npm i axios
in this case you are installing axios package in your local machine
and npx create-react-app 'app-name'
here you are executing the create-react-app package directly on your machine without installing it's files.
NPM stands for Node Package Manager.
It comes pre-installed with Node.js. NPM helps to manage packages in your projects as dependencies.
When using NPM, there are two ways to install a package into your local computer.
Locally: When a package is installed locally, it is installed in
./node_modules/.bin/ of the local project directory.
Globally: A global package is installed in the user environment
path. /usr/local/bin for Linux and AppData%/npm for Windows.
To execute a locally installed package, it should be specified in the package.json scripts block as shown below.
"scripts": {
"your-package": "your-package-name"
}
Then, you can execute the package with:
npm run your-package-name
NPX is an NPM package executor.
Currently, NPX is bundled with NPM when you install the NPM version 5.2.0 or higher.
Why NPX over NPM?
No need to edit the package.json file with node_modules paths.
You can directly execute the tool from the command line.
The differences between NPM and NPX are as below:
i) NPM is used to install the packages while NPX is used to execute the packages.
ii) Due to npm the packages installed have to be taken care of since it's installed globally while the packages used by npx don't need to be taken care of as they are not installed globally.
NPX is a tool for creating and executing some features in a new project
NPM is the package manager that contains all of libraries
Here is the simple definition.
NPM is a package manager, you can install node.js packages using NPM
NPX is a tool to execute node.js packages.
Here's an example of what your app creation might look like using npx
npx create-react-app project-name --template all
Simply npm is the Node Package Manager and
npx is the executeable version that run npm packages
npm is a tool that use to install packages and npx is a tool that use to execute packages.
npm-If you wish to run package through npm then you have to specify that package in your package.json and install it locally.
npx-A package can be executable without installing the package. It is an npm package runner so if any packages aren’t already installed it will install them automatically.
npm - package manager
npx - Execute npm package
This is a difference with it.
npm is package manager or installer on the other hand Packages used by npx are not installed globally so you have to carefree for the pollution for the long term.
Actually, I tried many ways to solve this and failed but finally removing/deleting yarn globally solves the problem
just type this command in your commandline terminal:
npm uninstall -g yarn
And then run the command below to install the react starter project
npx create-react-app

Cannot install my own npm package

I've been developing a later-to-be-release Open Source project with Node as a CLI tool. The CLI itself works great I only need to test if it works while on another project, so for that I installed the projects globally npm install -g without errors, but for the life of me I can't use the CLI.
I get the following error:
The odd thing is that the directory and file does exist in the global npm folder:
This is the project's package.json:
Am I not understanding how making a npm/node CLI works? What I'm missing?
EDIT 1:
This is my index.js file:
And this is the commander.js file:
EDIT 2:
After creating a test project as #AngYC suggested I could use the test cli successfully, while inspecting the difference I found this. Inside C:\Users\Ivan\AppData\Roaming\npm the .cmd of both projects are quite different:
EDIT 3 (Solution):
After fiddling around I found out that the file that really needed the shebang (#!/usr/bin/env node) was only index.js file and not the commander.js one. Removing the shebang in that file solved the problem
You may want to try to link your local package to your global executable list.
https://docs.npmjs.com/cli/link
All you have to do is run npm link in the folder you got your tool and it should make the command available globally.
Try to uninstall cli run npm rm -g cli or sudo npm rm -g cli. Then you run: npm install cli -g
If the problem persist, you might want to remove you npm package globally, probably there might be some conflicting things running.
Type this: %appdata% (either in explorer, run prompt, or start menu).
You can simply remove all globally installed npm packages by deleting the contents of:
C:\Users\username\AppData\Roaming\npm
Then you might also want to clear all the your cache run npm cache clear or npm cache clear --force as the case might be.
Then you reinstall all your packages that were install globally again.
If problem still persist, check this:
When you run npm root -g, it yields C:\Users\<username>\AppData\Roaming\npm\node_modules, or even, you should check your path maybe the executable binaries and .cmd files end up in C:\Users\<username>\AppData\Roaming\npm instead of C:\Users\<username>\AppData\Roaming\npm\node_modules, you will need to add that path to the PATH env. variable.
I hope this resolves your issue.

Install ReactJS without internet [duplicate]

I have a project which I will have to deploy to client Windows systems where it will not be possible to connect to internet. I currently have a folder in D:\NODE which contains node.exe and npm.cmd and a node_modules folder. To be able to run node from command line I have added D:\NODE to PATH variable.
I can have most of the modules installed locally inside node_modules of my project. However there's one - node-windows - which needs to be installed globally to work.
Following suggestion below I went to node-windows (installed globally) and packaged it up (npm pack), which created a tarball. I have then copied that file with my project and tried to install it on the test machine globally like this: npm install -g node-windows-0.1.5.tgz
I can see that it got installed in the global directory. However when I try to run the command which uses this module it complains that it cannot find it: Error: Cannot find module 'node-windows'
When I list the modules (npm list -g) it is clearly there in the list...
What do you think? And thank you.
You can install packages on a system without internet connection by packing them using built-in functionality in npm. This way, the node modules will be installed properly.
Create a package.json.
In your package.json, list all the modules you need under bundledDependencies (docs on npm).
Run npm install to install your node files before packing.
Create a tarball with npm pack.
Copy the tarball over to the machine without internet connection.
Install the modules with npm install <filename>.
Update
Regarding your comments, it looks like your globally installed node modules isn't found.
Try using the npm link command (docs on npm link):
cd yourAppFolder
npm link node-windows
1 - In system with internet access install module with this command:
npm install [module name]
2 - go to %userprofile%\AppData\Roaming\npm\node_modules[module name]\
(e.g C:\Users\janson\AppData\Roaming\npm\node_modules\grunt-cli)
3 - run npm pack
4 - this should result in a [module name]-x.y.z.tgz file
5 - run npm i -g [module name]-x.y.z.tgz in offline system

Default directories for Node.JS, Karma, and Mocha on all operating systems [duplicate]

Can someone tell me where can I find the Node.js modules, which I installed using npm?
Global libraries
You can run npm list -g to see which global libraries are installed and where they're located. Use npm list -g | head -1 for truncated output showing just the path. If you want to display only main packages not its sub-packages which installs along with it - you can use - npm list --depth=0 which will show all packages and for getting only globally installed packages, just add -g i.e. npm list -g --depth=0.
On Unix systems they are normally placed in /usr/local/lib/node or /usr/local/lib/node_modules when installed globally. If you set the NODE_PATH environment variable to this path, the modules can be found by node.
Windows XP - %USERPROFILE%\AppData\npm\node_modules
Windows 7, 8 and 10 - %USERPROFILE%\AppData\Roaming\npm\node_modules
Non-global libraries
Non-global libraries are installed the node_modules sub folder in the folder you are currently in.
You can run npm list to see the installed non-global libraries for your current location.
When installing use -g option to install globally
npm install -g pm2 - pm2 will be installed globally. It will then typically be found in /usr/local/lib/node_modules (Use npm root -g to check where.)
npm install pm2 - pm2 will be installed locally. It will then typically be found in the local directory in /node_modules
The command npm root will tell you the effective installation directory of your npm packages.
If your current working directory is a node package or a sub-directory of a node package, npm root will tell you the local installation directory. npm root -g will show the global installation root regardless of current working directory.
Example:
$ npm root -g
/usr/local/lib/node_modules
See the documentation.
For globally-installed modules:
The other answers give you platform-specific responses, but a generic one is this:
When you install global module with npm install -g something, npm looks up a config variable prefix to know where to install the module.
You can get that value by running npm config get prefix
To display all the global modules available in that folder use npm ls -g --depth 0 (depth 0 to not display their dependencies).
If you want to change the global modules path, use npm config edit and put prefix = /my/npm/global/modules/prefix in the file or use npm config set prefix /my/npm/global/modules/prefix.
When you use some tools like nodist, they change the platform-default installation path of global npm modules.
On windows I used npm list -g to find it out. By default my (global) packages were being installed to C:\Users\[Username]\AppData\Roaming\npm.
If you are looking for the executable that npm installed, maybe because you would like to put it in your PATH, you can simply do
npm bin
or
npm bin -g
If a module was installed with the global (-g) flag, you can get the parent location by running:
npm get prefix
or
npm ls -g --depth=0
which will print the location along with the list of installed modules.
Not direct answer but may help ....
The npm also has a cache folder, which can be found by running npm config get cache (%AppData%/npm-cache on Windows).
The npm modules are first downloaded here and then copied to npm global folder (%AppData%/Roaming/npm on Windows) or project specific folder (your-project/node_modules).
So if you want to track npm packages, and some how, the list of all downloaded npm packages (if the npm cache is not cleaned) have a look at this folder. The folder structure is as {cache}/{name}/{version}
This may help also https://docs.npmjs.com/cli/cache
In earlier versions of NPM modules were always placed in /usr/local/lib/node or wherever you specified the npm root within the .npmrc file. However, in NPM 1.0+ modules are installed in two places. You can have modules installed local to your application in /.node_modules or you can have them installed globally which will use the above.
More information can be found at https://github.com/isaacs/npm/blob/master/doc/install.md
To get a compact list without dependencies simply use
npm list -g --depth 0
The easiest way would be to do
npm list -g
to list the package and view their installed location.
I had installed npm via chololatey, so the location is
C:\MyProgramData\chocolatey\lib\nodejs.commandline.0.10.31\tools\node_modules
C:\MyProgramData\ is chocolatey repo location.
As the other answers say, the best way is to do
npm list -g
However, if you have a large number of npm packages installed, the output of this command could be very long and a big pain to scroll up (sometimes it's not even possible to scroll that far back).
In this case, pipe the output to the more program, like this
npm list -g | more
I was beginning to go mad while searching for the real configuration, so here is the list of all configuration files on linux:
/etc/npmrc
/home/youruser/.npmrc
/root/.npmrc
./.npmrc in the current directory next to package.json file (thanks to #CyrillePontvieux)
on windows:
c/Program\ Files/nodejs/node_modules/npm/npmrc
Then in this file the prefix is configured:
prefix=/usr
The prefix is defaulted to /usr in linux, to ${APPDATA}\npm in windows
The node modules are under $prefix tree, and the path should contain $prefix/bin
There may be a problem :
When you install globally, you use "sudo su" then the /root/.npmrc may be used!
When you use locally without sudo: for your user its the /home/youruser/.npmrc.
When your path doesn't represent your prefix
When you use npm set -g prefix /usr it sets the /etc/npmrc global, but doesn't override the local
Here is all the informations that were missing to find what is configured where. Hope I have been exhaustive.
You can find globally installed modules by the command
npm list -g
It will provide you the location where node.js modules have been installed.
C:\Users\[Username]\AppData\Roaming\npm
If you install node.js modules locally in a folder, you can type the following command to see the location.
npm list
Echo the config: npm config ls or npm config list
Show all the config settings: npm config ls -l or npm config ls --json
Print the effective node_modules folder: npm root or npm root -g
Print the local prefix: npm prefix or npm prefix -g
(This is the closest parent directory to contain a package.json file or node_modules directory)
npm-config | npm Documentation
npm-root | npm Documentation
npm-prefix | npm Documentation
Expanding upon other answers.
npm list -g
will show you the location of globally installed packages.
If you want to output that list to a file that you can then easily search in your text editor:
npm list -g > ~/Desktop/npmfiles.txt
From the docs:
In npm 1.0, there are two ways to install things:
globally —- This drops modules in {prefix}/lib/node_modules, and puts executable files in {prefix}/bin, where {prefix} is usually
something like /usr/local. It also installs man pages in
{prefix}/share/man, if they’re supplied.
locally —- This installs your package in the current working directory. Node modules go in ./node_modules, executables go in
./node_modules/.bin/, and man pages aren’t installed at all.
You can get your {prefix} with npm config get prefix. (Useful when you installed node with nvm).
From the docs:
Packages are dropped into the node_modules folder under the prefix.
When installing locally, this means that you can
require("packagename") to load its main module, or
require("packagename/lib/path/to/sub/module") to load other modules.
Global installs on Unix systems go to {prefix}/lib/node_modules.
Global installs on Windows go to {prefix}/node_modules (that is, no
lib folder.)
Scoped packages are installed the same way, except they are grouped
together in a sub-folder of the relevant node_modules folder with the
name of that scope prefix by the # symbol, e.g. npm install
#myorg/package would place the package in
{prefix}/node_modules/#myorg/package. See scope for more details.
If you wish to require() a package, then install it locally.
You can get your {prefix} with npm config get prefix. (Useful when you installed node with nvm).
Read about locally.
Read about globally.
Windows 10: When I ran npm prefix -g, I noticed that the install location was inside of the git shell's path that I used to install. Even when that location was added to the path, the command from the globally installed package would not be recognized. Fixed by:
running npm config edit
changing the prefix to 'C:\Users\username\AppData\Roaming\npm'
adding that path to the system path variable
reinstalling the package with -g.
In Ubuntu 14.04 they are installed at
/usr/lib/node_modules
For Windows 7, 8 and 10 -
%USERPROFILE%\AppData\Roaming\npm\node_modules
Note:
If you are somewhere in folder type cd .. until you are in C: directory. Then, type cd %USERPROFILE%\AppData\Roaming\npm\node_modules. Then, magically, %USERPROFILE% will change into Users\YourUserProfile\.
I just wanted to clarify on ideas referred by Decko in the first response. npm list -g will list all the bits you have globally installed. If you need to find your project related npm package then cd 'your angular project xyz', then run npm list. It will show list of modules in npm package. It will also give you list of dependencies missing, and you may require to effectively run that project.
Btw, npm will look for node_modules in parent folders (up to very root) if can not find in local.
If you're trying to access your global dir from code, you can backtrack from process.execPath. For example, to find wsproxy, which is in {NODE_GLOBAL_DIR}/bin/wsproxy, you can just:
path.join(path.dirname(process.execPath), 'wsproxy')
There's also how the npm cli works # ec9fcc1/lib/npm.js#L254 with:
path.resolve(process.execPath, '..', '..')
See also ec9fcc1/lib/install.js#L521:
var globalPackage = path.resolve(npm.globalPrefix,
'lib', 'node_modules', moduleName(pkg))
Where globalPrefix has a default set in ec9fcc1/lib/config/defaults.js#L92-L105 of:
if (process.env.PREFIX) {
globalPrefix = process.env.PREFIX
} else if (process.platform === 'win32') {
// c:\node\node.exe --> prefix=c:\node\
globalPrefix = path.dirname(process.execPath)
} else {
// /usr/local/bin/node --> prefix=/usr/local
globalPrefix = path.dirname(path.dirname(process.execPath))
// destdir only is respected on Unix
if (process.env.DESTDIR) {
globalPrefix = path.join(process.env.DESTDIR, globalPrefix)
}
}
If you have Visual Studio installed, you will find it comes with its own copy of node separate from the one that is on the path when you installed node yourself - Mine is in C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VisualStudio\NodeJs.
If you run the npm command from inside this directory you will find out which node modules are installed inside visual studio.

Categories