How to create an NPM environment for an offline build - node.js

I am trying to do an NPM build, install and bundle on a server which does not have a network connection
I can run the build successfully on an online server, and I can copy the directories which are required over to the offline server
How can I reproduce the NPM environment on the offline server so that an an NPM build, install and bundle will be successful?
I assume I should be copying the node_modules and package-lock.json and running an npm install --offline.

I can run the build successfully on an online server, and I can copy the directories which are required over to the offline server
Knowing that, on a system that has access to the internet, run npm install to create and populate the node_modules directory with all the dependencies.
Copy over the entire application directory including node_modules directory, allowing all dependencies available to the offline server.
This is the approach shared in comments: How to install npm package while offline?
If the dependencies need to be updated, these steps would need to be repeated.

Related

how to use npm to install all node modules in local network?

My node app will deploy in a bank that can't access the internet. Can I download all the node modules in a local directory, and install them from it, or just copy the node_modules to the bank machines? Any safe and practical advise will be highly appreciated, thank you.
OR
How to add node_modules dependencies without using internet .
Add offline-npm to your project to serve a npm compatible tgz file wich contains all dependencies for offline installation with npm install.
Additionally you can use offline-npm -n to install packages from your local npm cache directory
For installation
npm install -g offline-npm
Usage
1) Open terminal and go to your project you want to prepare for offline use. This folder needs to contain a package.json file.
2) Prepare your project for offline use
offline-npm --add
3) This changes the package.json file and adds a offline folder which will contain all your dependencies.
Pack your project
npm pack
For more details , visit the Link
Hope this answer gets useful to you .
Typically you will want to bundle your node_modules into your deployable artifact.
There are some gotchas here around any native dependencies as you need to match the nodejs version of the build environment with the target system.
The easiest way to achieve this in my experience is by using docker to build and package your deployment. Though it is possible to do when running directly on host machines, you may find it safest to just avoid the usage of native dependencies to remove any risk of things breaking from a nodejs or os update.
I've also successfully achieved this packaging the nodejs binary into my deployment artifact that was deployed directly on centos hosts, however we had a mixture of centos 6 and centos 7 hosts at the time, which brought additional complexity related to different glibc versions causing nodejs to fail to start with the system provided library.
In short if you can I would use docker to package your application into a completely self contained image.
you can just copy the node_modules into the deployment machine but it is bad practice.
there are other solutions like using local-npm package... your npm installs are fetched from the registry and then modules and their dependencies get stored in a local PouchDB database. This caches them so subsequent npm installs use the local cache rather than calling to the network. local-npm also takes care of keeping modules updated when they change. It does this by listening for changes to the remote registry so you don't have to worry about staleness.
or bundling your packages to use them offline you could visit this link for more details for offline installation of npm packages:
https://addyosmani.com/blog/using-npm-offline/
How to install node modules in local network?
1. Solution: yarn offline and 'Offline Mirro'
One of the main advantages of Yarn is that it can install node_modules from files located in file system. We call it “Offline Mirror” because it mirrors the files downloaded from registry during the first build and stores them locally for future builds.
2. New problem: How to use yarn in local network?
Download yarn.tar.gz into the local repository, and install it in local node_modules directory.
npm install yarn.tar.gz --no-save
3. Usage
# run yarn install, and download the node modules (.tar.gz) into the offline mirror directory '$REPOSITORY/yarn/yarn-offline-mirror'.
npm run online-install
# with yarn.lock file, install node_modules from offline mirror directory '$REPOSITORY/yarn/yarn-offline-mirror'
npm run offline-install
4. Problems
4.1. problem 1
4.1.1. description
error can`t make a request in offline mode("http://....")
4.1.2. reason
the indirect dependencies could not be downloaded into offline mirror directory
4.1.3. resolution
yarn config set yarn-offline-mirror-pruning false
5. Github demo
yarn-offline-deploy-demo
6. Reference
1. Running Yarn offline

Meteor build with shrinkwrap

I am trying to build and deploy a Meteor project. My constraint is that I have to deploy the app on a remote computer of my local network without internet, so the npm install should not need to connect anywhere.
My idea is to use shrinkwrap and shrinkpack as described here:
https://guide.meteor.com/using-npm-packages.html#npm-shrinkpack
Then build as described here:
https://docs.meteor.com/commandline.html#meteorbuild
What I do on the dev box:
meteor npm shrinkwrap // this properly updates the npm-shrinkwrap.json
meteor npm shrinkpack // this properly populates the \node_shrinkwrap folder with the tars of the dependencies
meteor --production
//terminate meteor
meteor build ..\build // I cannot find the tars for the dependencies in the output
Then I copy the tarball to the remote computer:
tar -zxf coolMeteorAngular2App.tar.gz
cd bundle\programs\server
npm install // still connects to internet to get the dependencies instead of getting the from the local bundle
node main.js
Does anyone knows what I am doing wrong? How do I include the dependencies tarballs into the build, and how do I tell npm install to use them?

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 !

can I bundle node_modules and do npm install from the bundle?

I am trying to setup Jenkins to do tsc but the build machine will not have internet access. To facilitate this I need to bundle the node_modules from another box with internet access and copy it over to the build machine and use this bundle to do npm install. In effect, I am trying to replicate maven's local repository structure to prevent npm install from accessing npm's repository. Is there a way to achieve this? From what I saw, npm pack facilitates deployment but not what I am trying to achieve, I need node_modules so I can compile on the build machine. Also, it is a windows machine so no luck with offline-npm.

Unable to install Socket.IO disconnected from internet

I need to install Socket.io on a machine without internet access.
I've downloaded Node.js and Socket.IO on another box, but when I copy and try to install them on the isolated machine, Node.js installs ok, but Socket.IO insists on connect to GitHub.
How can I install Socket.IO without an internet connection? Should I install all dependencies offline? If so, what are the dependencies of Socket.IO?
It turns out that npm supports package caching. Basically you create a cache on the development machine that does have internet access, copy that cache onto your target at the same time that you install your nodejs application, and then install the packages from the cache. I assume from your question that the target machine already has nodejs and npm installed.
Step 1. Use npm to create a cache directory on your development machine
First, create a cache directory and configure npm to use it. Then install each of your packages.
mkdir ../my-cache
npm config set cache ../my-cache
npm install --save async#0.9.0
npm install --save restify#2.8.3
etc.
If you look in the my-cache directory you'll see sub-directories for each installed package.
Step 2. Copy the cache to your target machine along with your node application
No rocket science here: make sure you copy the my-cache directory to your target machine.
Step 3. Use npm to install the packages from the cache
Configure npm to use the cache directory. Be aware that npm will still try to go fetch the package files form the internet. And it will retry after a failure. I found a couple recommendations for forcing npm to use the cache, but those options did not work. But I did find a way to significantly reduce the amount of time npm spends trying to fetch before looking in the cache.
npm config set cache ../my-cache
npm config set fetch-retries 1
npm config set fetch-retry-maxtimeout 1
npm config set fetch-retry-mintimeout 1
npm install async#0.9.0
npm install restify#2.8.3
Be aware that you cannot just type npm install because npm will not use the cache. This is a bit of a pain. If you want a robust install you can write a tiny nodejs app to parse out the dependencies and calls child_process.exec to install each one.
(*) I should mention that there is a package called npm-cache (https://www.npmjs.com/package/npm-cache). In my case npm-cache did not suit my needs. But you may be able to make it work for you.

Resources