Is it possible to install global node packages offline? - node.js

I need to be able to install the carto module on an offline server. Is there a way that I could package up carto with all of its dependencies, and install it on a server that has no connection to the internet.
The server won't have an initial connection, and will have npm and node installed from a .deb archive.
I've tried using npm-offline, as well as npm-offline-packer. These both require that I have an npm registry or a node project.
I'm hoping to have a start script that can run the required commands and get all packages installed. So far, I'm able to install all ubuntu software, just stuck on node.

An alternative would be installing it in another machine and copying the package(s) you want inside npm's global node_modules.
npm config get prefix
Gets the path to where it is installed. node_modules are usually under lib/ folder. Module executables could be located under bin/. Having both should be enough to use your global module in another machine.
Since you're looking for a start script the steps you need are:
Getting npm prefix via npm config get prefix
Go to that path
Copy executables you want under bin/ i.e. carto#
Copy content you want from lib/node_modules i.e. lib/node_modules/carto
Apply to the machine you want using the same steps described here

Related

npm install command - please explain

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.

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 !

What is the difference between installing a package locally and globally using npm?

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.

NPM on OSX, error getting global installed packages

I have nodejs installed on a MBP which runs OSX 10.9, I have installed as a package downloaded from the nodejs website. Then I have installed the MEAN stack following instructions on mean.io.
The commands are:
sudo npm install -g mean-cli
mean init yourNewApp
That works correctly
Now the real issue is when after my app is created I enter the dir using the terminal, and write gulp, and it thows me some errors that some mandatory modules are not found.
The modules are written in the package.json file that mean generated, and they are installed as global modules on ~/.npm
I browsed the folder and there are all the required packages folders, inside the folders there is a package.tgz file which has the code of the package and a package folder which holds a package.json file describing the package itself.
Now I don't understand why the packages are compressed and why if they are installed globaly can not be accessed from gulp on my project folder.
Thanks in advance.
If you install some global module then you better don't put it into the package.json of your app because when you run your app that's the first place where is going to search and if it is there your app is going to look at node_modules folder and if it is not there your app will crash.
My advice is try to install your modules inside your app, npm install your_module --save because your app is gonna be portable and with a simple npm install you will be able to install all your needed packages.
But if you still wanna install global packages you maybe wanna follow this rules:
If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.
If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.
If you have the time to read the link then you will see that there are exceptions and how to handle them.

Difference between ~/.npm, $PROJECT/node_modules, and /usr/lib/node_modules?

I installed npm and when I did my first sudo npm install some-package -g it installed that package to /usr/lib/node_modules as I expected, but then it also created several files in ~/.npm. What's the difference between these locations?
Other answers here have said that a global installation using -g should install it to your home directory by default, but for me it installs it to /usr/lib/node_modules, am I doing something wrong?
And when I do a local installation without -g it installs to the current directory $PROJECT/node_modules. What's the difference between all these locations, and what should go where?
The system wide package install directory, typically under /usr/lib is usually used for globally installed packages that provide a binary which should be available in your PATH (to be able to execute it from anywhere).
The local install directory node_modules, created by npm install at the location where you're executing npm, is typically located in your project directory and usually used for project specific dependencies.
The ~/.npm contains packages already downloaded. When installing the same package in another location, npm will lookup that package in that cache directory first.
Reference: https://docs.npmjs.com/files/folders
Related files:
.npmrc - NPM configurations at different locations
package.json - Packages and their versions for a project
Hypothetical scenario: two projects using Grunt (a Javascript based buildscripting tool):
Both projects use different Grunt versions. One project is older. Grunt can't be updated without having to adapt the whole build process, another project has just started.
You have to install "grunt-cli" system wide (using the -g flag) since it provides the grunt binary. This CLI binary will lookup for a local "grunt" in your current project directory. The "grunt" npm on the other hand, installed locally (without -g) will then be bootstrapped by the CLI. When downloading grunt for the first project npm will store downloaded packages in ~/.npm, when installing grunt for the second project, npm will lookup packages common to both projects in ~/.npm first.
There are other reasons to install packages globally, the most time they provide a binary that should be located in your PATH.
Alternatively, some packages that typically need to be installed globally can also be installed locally. You'll then have to add the path to that binary (e.g. path/to/your/node_modules/.bin/<BINARY>) to your PATH variable or just specify the full execution path.

Resources