Should I install node.js modules through npm install only? - node.js

Is that any difference npm install and just move whole module folders? In fact, I'm trying to deploy my node app to linux server, but there is some problem with npm install so I move my node_modules folder entirely to linux server, apparently no problem with it.
Is it possible to be troubled this way later?

npm install doesn't just copy code from the Internet to node_modules. The installation might also compile code for the platform.
So if you are copying from/to the same platform it should be OK. Though even then some modules might not work depending on the environments.

npm install read package.json and install all missing modules in node_modules folder. There is no problem if you just copied node_modules folder from your source.
But if you want to install any new module then use:
npm install package-name --save (or -g if you want to install globally) so that your package.json can track new modules.

Related

one dedicated directory for node_modules

I am quite new to npm but I have a question about npm node modules. I have the node modules now installed in several directories. One for Gulp, one for nodeJS etc. I was wondering how I could have one dedicated node modules directory and let the other packages point to that directory, instead of having node modules all over the place.
What is best practise?
Cheers,
Lino
Yes, you can have global node_modules directory accessible to multiple application.
A package is available globally if it is installed with -g option. For instance if body-parser is required globally it should be installed with the following npm command:
npm install body-parser -g
Following are the paths are usage of modules:
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.

How to install npm package while offline?

I'm working on an offline network and want to install angular-cli using npm.
I have a zip file of angular-cli and using the latest node and npm version.
I'm using the command: npm install ./angular-cli-master to install angular-cli from the folder.
But I keep getting this error telling me I don't have an internet connection (which is ok).
So how can I install this angular-cli while offline using the zip I downloaded from Github?
Thanks for your help.
You simply copy the package and all dependencies in your node_modules folder, inside the project for local installation, or in the global folder (npm config get prefix to see where it is located) for a global installation.
The behavior of npm install is to check for the dependencies, and install them first. When it doesn't find them installed, nor the local file containing them, it tries to download them.
Since all of those steps fail (you don't have the dependency installed, it isn't available on the expected location, and it can't download it), the installation fails.
You can find the dependency list in the package.json of each module, but since it is recursive, it can take a long time to have everything set right if you do it manually, npm does it by recursion.
For you, the easiest way would be to create a new folder on the connected PC, and inside it npm install angular-cli, zip the folder and transfer it on the offline machine.
Jan 2016 - check out Addy Osmani's recommendations for offline installation of npm packages
May 2017 - as of npm 5, you can pass the --prefer-offline flag to npm install
yarn does this out of the box.
In 2019, I found none recommended approaches were applicable to an "air gapped" server with no internet access.
I found the only solution was to, on windows, using artillery.io as an example:
install the package on a machine with internet access, e.g local dev machine. npm install -g artillery
Browse to C:\Users\{username}\npm
zip up the \node_modules\artillery (e.g artillery.7z)
Copy the zip and the files artillery, artillery.cmd (at root of npm folder) to the server
Paste the two artillery, artillery.cmd to the root of the servers npm folder (C:\Users\{serverusername}\npm)
Extract the zip to C:\Users\{serverusername}\npm\node_modules
This is the complicated version for just one tool. If your local machine's npm folder is relatively light on tools, you could always just zip the whole npm folder and copy + extract it on the server.
I still think it's odd that npm insists on trying to connect to the registry even when using npm pack and npm install -g <tarfile>
Problem: I'd been in similar situation where I can't install the express.js and all other dependencies specifies by package.json on my local machine (offline) using npm due to unavailability of internet connectivity.
Solution: I've a solution that works on Windows(not so sure of other platforms) through which I installed express framework with all the dependencies I required for my project which include cookie-parser, jade, morgan etc.
Steps :
Install all the package(s) on a remote machine which has an internet access.
In my case I'm using Windows on both remote as well as local machines and my requirement was of installation of express.js on local machine . So I run below command on my remote machine to install express.js
C:\Users>npm install -g express-generator`
After installation of express.js I created an app on my remote machine using:
C:\Users\Name\Desktop>express Project`
C:\Users\Name\Desktop\Project>npm install -g =>to install all other dependencies globally*
Now browse to location where npm's global modules are stored, you can view the location by
C:\Users>npm config get prefix
Generally in Windows its
C:\Users\{Username}\AppData\Roaming\
Simply copy the npm and npm-cache folder of your remote machine.
And place both copied folders viz. npm and npm-cache into your local machine on same location thats
C:\Users\{Username}\AppData\Roaming\
the short answer, you can't. Most NPM packages such as #angular/cli need other dependencies and those need child dependencies which get installed when you run npm install
You can, however, install the cli when on the network and use it when offline.
You can find the npm install command documentation here: https://docs.npmjs.com/cli/install
I am not quite sure and unfortunately, I do not have the chance to test it myself right now, but I would try to either unzip the folder and remove the dot, like that:
npm install /angular-cli-master
(= installing a folder not a zip file)
or just add the zip file ending like that:
npm install ./angular-cli-master.tgz
(= installing a zip-file not a folder, file ending may be .zip or something else, though)
Was test success with node 18.x.x.
The following step guild how to install http-server package
On Online PC:
npm install -g http-server
After finish install, copy http server folder. (Usually locate at: C:\Users[UserName]\AppData\Roaming\npm\node_modules)
On offline PC:
Paste http-server folder. e.g. D:\http-server
npm install -g D:\http-server
Online computer:
npm install -g offline-npm
copy the npm-module to the offline computer and thats it !

How do I install all the requirements with npm?

I would like to clone https://github.com/tstringer/create-react-app-with-redux and start a new project. I ran npm start and then ran npm install for each module not present, but there are many of them. Is there a way to install all the requirements? Something like pip install -r requirements.txt in Python.
Thanks,
Uri.
Just run npm install without arguments. It will resolve the required dependencies from the package.json file.
It's simple.
If you want to install all the node_modules from the package.json file you simply put: npm install in terminal (on the same directory where the package.json exists) and it would install all the node modules in the folder called node_modules.
Generally, the node_modules folder is not uploaded in a git (by putting restriction at .gitignore) because it is essentially the same folders or packages that one would have to install, *hence installing it from package.json is simpler and it saves the internet bandwidth and time.
Even you want to save something in the package.json while you are installing any npm package you can simply put npm install --save your-package-name and it would automatically save your package in the .package.json file and you can install the same file, even after you delete the node_modules folder using the same command.
Better yet, if you want to save yourself a lot of time use yarn install instead of npm install (https://yarnpkg.com/en/). It is much faster because it caches everything and operates in parallel (see https://www.sitepoint.com/yarn-vs-npm/ for a good comparison).
npm install githubname/reponame -- Repository Name you can try

Homebrew Install Node Location

Kinda 2 questions related to each other here.
When doing a brew install node, should I first navigate to the root of whatever folder I'm going to hold all my future web projects/apps then run it? Or does it not matter where I run the install initially for Node? Because I notice it creates a node_modules folder in /local/lib/node_modules
I assume it doesn't matter, and when you start installing node packages using npm install [package] it'll create a separate node_modules folder under the context you're in so lets say /www/MyApplication run npm install and it'll create /www/MyApplication/node_modules....and that the one under /local/lib/node_modules just serves as the one for npm itself because it needs its own root node_modules folder which is how npm runs?
Correct, it makes no difference where you run brew install node, it will install into your Homebrew folder.
When you use npm to install a Node module, it will install into the current directory, unless you use the -g global flag. Normally you will install modules that are project dependencies into your project folder, and global modules are for global utilities.
For example, to use Grunt you would install the grunt-cli package globally for the command-line utility.
npm install -g grunt-cli
And for each project that uses Grunt you will install a version of the grunt module for use with the project.
npm install grunt

How do you include npm libraries in a node.js app

I'm trying to create a little app with node-ncurses which I installed over npm install ncurses
with this library install i'm trying to run the examples for node-ncurses from the following
https://github.com/mscdex/node-ncurses/tree/master/examples
But I get path errors with the examples for require('ncurses'), what is wrong?
My ncurses library is install into ~/.npm/ which seems correct to me.
You want to be installing them locally into the same folder of your project. If you miss out the -g flag and just run npm install ncurses within your project directory, you should then be able to run require("ncurses") just fine. All NPM modules installed locally goes into a node_modules folder within your project.
A little further hint, if you install with:
npm install ncurses --save
That will add ncurses to your package.json as a dependency, which means any other dev who might check out your project can run npm install in the project's directory and automatically get ncurses installed as it's listed in package.json as a dependency.
Without the -g flag anything you install with NPM with install into a node_modules folder relative to where you ran the command.
My first recommendation would be to make sure that you are in the working directory of your project and then install ncurses again.
Here is an old, but relevant blog post about how it was designed.

Resources