node.js / npm - override local default modules directory (node_modules) - javascript

Let's say I don't want to install my local (per project) packages in node_modules - I'd like to have it under sources/node_modules instead of just node_modules. Is it possible to override it just like you can do in bower?
In bower you provide .bowerrc file with directory option, see docs. It works exactly as if you had bower_components locally - no additional files, loaders, nothing at all - just the container dir is different.

tl;dr
$ mkdir -p sources
$ ln -s package.json sources/package.json
$ npm install --prefix sources/
$ export NODE_PATH="`pwd`/sources"
Explanation
You can (as #adeneo mentioned) simply install packages to any folder with:
$ npm install --prefix sources/ my-package
This will install to sources/my-package. However, this solution is far from neat in two ways:
package.json
It sounds as though, rather than installing individual packages, what you really want to do is install everything in package.json to the sources/ folder. The problem is that when you do npm install --prefix sources/, it also looks for package.json in the sources/ folder. If that works for you, then great.
The only way I've found to keep your package.json in your project root and install node modules somewhere else is to symlink the local package.json into that directory:
$ mkdir -p sources
$ ln -s package.json sources/package.json
$ npm install --prefix sources
require
As you mentioned, you will probably want your scripts to be able to require modules like normal - require('my-module') rather than require('sources/my-module').
The NODE_PATH environment variable can help here:
$ export NODE_PATH=`pwd`/sources/node_modules
$ node -e "require('my-module')" # Success
There is an important caveat: node will look for modules in a specific order:
In a node_modules folder in the local directory
In a node_modules folder in the parent directory, then its parent etc.
In paths in the NODE_PATH, if it hasn't found the module yet
So be careful there are aren't any other node_modules folders in the current or any parent directory which mention your modules, or things could get quite confusing.

Create local_modules in your root directory and add package.json "#amazon/product-module": "file:local_modules/#amazon/product-module",
file structure
local_modules
#amazon (folder)
product-module (folder)
[add your file which overrides]
Now do npm update and you will get your local_modules/#amazon/product-module file into node_module/#amazon/product-module

Related

How to install node dependencies for custom package

i'm making a custom package for a project that i'm making, but it requires typescript to be installed as a local dependency...
npm i typescript #types/node ts-node ts-node-dev
This installs it in a node_modules folder, as per usual. The problem is though is when i try to locally install the package...
npm i ../package
it automatically creates a node_modules folder, and then installs it there. The package is installed inside a node_modules folder, within a package folder, within another node_modules folder.
My question is: how do i install it in the parent node_modules folder as a dependency alongside the custom one?

How to install npm dependencies inside a sub directory?

I have a package.json in the root: "install": "cd packages/my-package && yarn".
When I run yarn run install all I'm asking is that it goes into this package, installs the node_modules and dependencies and that's it. For some reason no matter what I try (yarn workspaces/lerna/manually etc.) it is installing node_modules in all packages, including the root.
WHY?! Just let me install them separately :(
Anyone knows how to easily fix this?
To move the basefolder of node_modules, you will have to create a .yarnrc file in the same directory as your package.json file. Your .yarnrc file should look like
--modules-folder packages/my-packages
The resulting output will put all of your packages after my-packages
/packages/my-packages/WHERE_YOUR_PACKAGES_GO

Where to look for the library files after npm install

I am installing nodejs module(jasmine-ajax) in my local machine . I am expecting the entire libraries in git hub will be available in my local system.
But I am seeing only the main file in lib folder . Where the other dependend files will be placed.
npm install --save-dev jasmine-ajax
Executed the above command and I am not seeing all the files under source in my local view.
https://github.com/jasmine/jasmine-ajax
Thanks
When you install a module like you did, it is placed in node_modules folder.
Running npm install is not getting all file from github repository, but from architecture that is pushed to npm. In the jasmine-ajax github, you can see a file called .npmignore which contains src. This file works like .gitignore files, src folder is excluded when he pushed is module to npm.
File in lib folder is generated from src folder files, so it contains all you want
mock-ajax.js file, in lib folder, represents the built library. You don't need to have all files in src when you download it from npm. You can use all jasmine-ajax features in this way:
Install jasmine-ajax from NPM via npm install --save-dev jasmine-ajax;
you can then require('jasmine-ajax') inside your test-suite and access
it via the jasmine global.
When you launch a npm install without the -g flag the folder that you have to look for is node_modules in the path where you launched the command.
If you use -g flag it will be installed as a global module.
In your case the folder will be node_modules/jasmine-ajax.
The fact that the lib folder contains only the mock-ajax.js file depends on the release they made on npm registry.
The npm install command DOES NOT clone the github repository, it downloads the release from the npm registry: https://www.npmjs.com/package/jasmine-ajax

Is there a minimal version of "npm install grunt"?

When I run npm install grunt I get, in my opinion, a heavily polluted project folder. In the folder node_modules there are libs like rimraf, coffescript and others I really don't want to use. Are these required for grunt itself to be able to run or are they only there for my convenience?
I would like some kind of "npm install grunt --minimal" to only install an absolute minimum of what's needed to be able to use grunt. I don't really like my project folder full of stuff that's not really my own code. I've googled the terms i can come up with, like "minimal grunt install", but only found the basic install tutorials.
Create a node_modules directory in a directory above your project's directory and do npm install grunt there. That way the modules are accessible but they aren't in your project's node_modules directory.
Another option is to use npm install -g grunt to install grunt globally.
No, you can not. Grunt is npm package and it has dependencies (this is normal). The fact is this dependencies don't pollute your project folder structure. It is normal to
add node_modules to .gitignore and don't commit it
install every npm package with --save flag (will write package to package.json) or --save-dev flag (will write package to package.json as package needed for development)
commit package.json to repository. Thus, every team member can install all packages via npm install or npm install --production
As Dan said you can install grunt globally with -g flag but this will not solve your problem as every npm package that you install will have it own dependencies (and you'll probably think that it pollutes you folder structure as well)
You know, there are a lot of things that you need for development (or production). While installing apache (or something else) you don't say that is pollutes your file system (but it has own folder and so on).
Things don't pollute file structure if they don't exist in your project repository.
use pnpm
pnpm creates the "deep" node_modules that you want
also saves disk space and network traffic : )
alternative: npm install --legacy-bundling
to produce a deep node_modules

Where does npm install packages?

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

Resources