I would like to have my Node modules stored in a centralised place, in say, /var/http/common/ and have my app live/run in a different location, say /var/http/www/apps/APP#1_NAME/.
I was able to set up the requires in server.js to use relative paths like require('../../../common/express'), but from reading posts by NPM's author, it sounds like I'm hacking it, and I should use npm link to create a "local" reference for Node (which symlinks to the real installation).
I first installed my node modules in /var/http/common/, but then when I tried to create the symlink (npm link ../../../common/node_modules/express), npm seems to have treated it like a "global" install, and re-installed express in /usr/local/lib/node_modules/express (and created a "local" symlink to it ./node_modules/express ->) which is not what I expected to happen. Is this what I actually want? Should I have used npm config set prefix first?
Turns out: I should have set npm config set prefix before doing anything else.
It might appear that npm link and npm install -g do the same thing; however whilst, npm link will install the module globally, it also creates a local symbolic link in node_modules pointing to $prefix/lib/node_modules/$module. MaxGfeller is incorrect: Without that local symlink, Node will complain that it cannot find the (globally installed) modules. This is determined thru my own attempts as well as is inferred from npm help folders:
• Install it locally if you're going to require() it.
• Install it globally if you're going to run it on the command line.
• If you need both, then install it in both places, or use npm link.
This doesn't specifically address what I asked: I want to have the modules stored in a central location (to be accessed by multiple Node Applications) but I don't care about using them from the command like—I only want to use them in require('').
As for my question about using relative paths in require(''), I still have not got/found an authoritative answer for that, but it would seem from the existance of npm link that using rel paths is not the author's intent. To me it seems like a case of six-of-one, but I would like to remain consistent with Node's standard.
You can install your node modules globally using by adding '-g' to the command. For example:
npm install express -g
In your code you can use it normally:
require('express');
The modules are then stored in /usr/local/lib/node_modules
Related
Same title.
I don't want install every time with any project.
Just install once.
Thanks in advance.
Edit: I know install global but how to require it is my question.
Check Addenda: Package Manager Tips
Define NODE_PATH with path local modules, example:
/usr/lib/node_modules
C:\Users\<Username>\AppData\Local\Yarn\Data\global\node_modules
Or anywhere you want
And now you can call it.
Since the module lookups using node_modules folders are all relative, and based on the real path of the files making the calls to require(), the packages themselves can be anywhere.
Yes. possible. Install all the modules globally.
ie; npm install -g <module_name>
Then use it in your application as necessary.
But, this method is not advised.
After you install them global, use npm link in project folder with package names(see npm link), to link them to that project. e.g. if your project requires lodash use npm link lodash.
Another way, if you want to have multiple little scripts(e.g. not projects) you can set the NODE_PATH variable with path to where your npm stores global packages. And after that require('<global-module>') will work without link and installing node_modules into folder.
I've searched through other questions such as this one, but they all seem to be about a local npm link stopping working for another reason than mine. I assume this is a common use-case issue, so if I'm doing something methodically wrong, I'm more than happy to take suggestions on how I should be doing it.
Principally, I have a private npm module that I'm working on called #organisation/module. When working locally, I'll run npm link on it, and use it within my 'host' project as npm link #organisation/module — this all works great with hot-reloading, etc. I'll also import it as import module from '#organisation/module.
However, since I also want to publish my local changes to npm (as #organisation/module) from time to time, for build testing and production code, I need to run npm install #organisation/module on the host project.
This then seems to break the implicit npm link I set up earlier... I assume mainly because they are the same name, and npm favors an install over a link?
When I want to make live, local changes again, the only way I can currently get it to work is via npm uninstall #organisation/module and then to re-link it.
Is there a way to keep the published module installed (in order to avoid careless mistakes, like forgetting to reinstall it for build testing), but always favour the local, linked instance?
Diagram for ref:
Have you tried locally installing with the other method npm provides.
npm install /absolute/path/packageName
I believe this will change your entry in package.json to look like this:
"dependencies" {
...
"packageName": "file:../../path/to/packageName",
...
}
Since npm link creates a symlink in the global folder, while npm install is local to the project npm install takes precedence. You can read about npm link here: https://docs.npmjs.com/cli/link
To avoid this, my suggestion would be to use npm install <path to local> and when you need to use the production code use npm install #organization/module. This would update your node_modules per code basis. Read about npm install here: https://docs.npmjs.com/cli/install
Hope this helps :)
Go to the directory where your local package is located open package.json change the name from original_name to "original_name_local".
write npm link on terminal at the same location.
After this go to your working directory and write npm install <path to local>
Now whereever you're requiring or importing update the name to "original_name_local"
for example if it's require('space-cleaner') then change it to require('space-cleaner_local')
Like this you can have both local as well as production package just change the name wherever required.
Otherwise you can remove package by removing it from package.json and deleting from node_modules.
if local is needed go to local package directory and on terminal write npm link and then on your working directory write npm install ./path/to/package
if production then again delete the package as told above and write npm install package_name
I have used to install my nodejs on D:\ drive instead of C and have set environment variables to D drive node & npm folders.
Then i changed npm installation path as "prefix=D:\node\node_modules\npm
" on "npmrc" file. So i could confirm that all user based modules are pointing on D drive npm folder instead of appdata.
I tried to install express js globally and i used to check the package tree on my cli as mentioned below,
npm ll -g
while trying this command am getting npm extraneous ERR,
Please suggest me that which way i have to use npm path and installation stuffs.
Thanks in advance.
It might seem like a good idea to install packages globally, but this is one great reason not to.
Often used packages like express, and cookies should be kept local to a package. Mostly because of versioning issues. You might have one package using express2, but your new one wants to use express3. You would have trouble if it was a global install. When in doubt leave off that -g, and use a --save instead. (This adds the package to your npm dependencies list.)
On the other hand, command line tools like mocha, yeoman, and uh not much else that I know of should be installed with the -g flag.
I'm not much of a windows person, so you'll have to look a little yourself, but I would also recommend not installing Node by hand, but instead using a version manager like nvm to do that stuff. Here's an nvm port for windows: https://github.com/coreybutler/nvm-windows
I'm not regular node user, so my apologies if this is a stupid newbie question, but I haven't been able to find any clear documentation on this, and my feeble newbie node skills don't let me dig into myself.
I'm following along with these instructions for installing the Ghost blogging system, (a system built with NodeJS).
After telling me to open a terminal window in the just downloaded package folder, yhe instructions include the following line
In the new terminal tab type npm install --production
This confuses me. My understanding of npm is it's a package manager that, like perl's CPAN
Fetches packages from The Internet
Installs them into my local node system
That's clearly not what's happening above, but I don't know what is happening when I run that command, and since I don't run with a NodeJS crowd I don't know who to ask.
I'd like to know what NPM is doing. Specific questions
When I run npm install, it looks like it's downloading a number of packages (lots of npm http GET in the console). How does NPM know what to download?
Where is it downloading these module files to? How does npm know where to download the files?
What effect does the --production flag have on NPM's behavior?
Happy to have specific answers, or a meta-answer that points out where I can learn how npm works with (what appears to be) a application installs (vs. a system install, which is how I normally think of it)
npm has a few different installation modes. From within a module (with a package.json file) npm install installs the dependencies listed in the dependencies and devDependencies fields of the package.json file. Installation means that files the modules are downloaded, placed in the node_modules folder, then npm installed themselves, (but only their dependencies) placing modules their own node_modules folders. This continues until everything needed is installed. Use npm ls to see the tree of installed packages.
Most of the time this is what you want, because running npm install from within a module is what you would do when developing on it, and you'll want to run tests etc. (which is what devDependencies is for).
Occasionally though, you'll be coding a service that consumes modules, but should not necessarily be treated like one (not intended to be require'd). Ghost is such a case. In these cases, you need npm install --production, which only installs the dependencies, leaving the devDependencies.
When I run npm install, it looks like it's downloading a number of
packages (lots of npm http GET in the console). How does NPM know what
to download?
It reads the package.json configuration file in the current directory.
Where is it downloading these module files to? How does npm know where to download the files?
It will create and populate a node_modules directory within the current directory. The file structure is designed in to npm/node and is (mostly) intentionally not configurable.
What effect does the --production flag have on NPM's behavior?
Install just the dependencies without the devDependencies from package.json, meaning "give me what I need to run this app, but I don't intend do do development on this app so I don't need dev-only stuff".
npmjs.org has some docs, FAQ, and man pages, which are pretty good although they are mostly lacking basic introductory material.
I just setup official node.js on windows which includes npm in custom directory d:\myserver\nodejs
I tested npm with
npm install less
it works but I can't see any less directory in node_modules\npm\node_modules subdirectory.
Where could I find it (I guess the name of less module is actually less).
under : node_modules
just check where you where pointer "in what directory were you" at the time of the installation
d:\myserver\nodejs -> node_modules
Just for further clarification, google brought me here while looking up where global modules are stored (installed via npm install -g ...).
From the documentation
Local install (default): puts stuff in ./node_modules of the current package root.
Global install (with -g): puts stuff in /usr/local or wherever node is installed.
Install it locally if you’re going to require() it.
Install it globally if you’re going to run it on the command line.
If you need both, then install it in both places, or use npm link.