How to make npm install offline without internet

Note: This is a best way is combining both build cache offline to improve performance.

1.  On an online machine

Seting npm

# set cache path where we will clone to in ci
npm config set cache ~/npm_cacache/.npm
 
# set default is using cache and if failed will fall back to registry
npm config set prefer-offline true
 
# enable npm log
npm config set loglevel verbose

# set internel registry : Optional, if set we need to set it same value for online machine
# npm config set registry https://<your-nexus/repository/npm-group>
npm config set registry https://registry.npmjs.org
 
cat ~/.npmrc

registry=https://<your-nexus/repository/npm-group>
prefer-offline=true
cache=/home/<user>/npm_cacache/.npm
loglevel=verbose

This downloads and caches all packages into .npm

# clean and rebuild all project
make clean
make

 

2. On the offline machine

# set cache path where we will clone to in ci
npm config set cache ~/npm_cacache/.npm
 
# set default is using cache and if failed will fall back to registry
npm config set prefer-offline true
 
# enable npm log
npm config set loglevel verbose
 
# set internel registry same as in online machine
# npm config set registry https://<your-nexus/repository/npm-group>
npm config set registry https://registry.npmjs.org
 
# set certificate
npm config set cafile /etc/ssl/certs/ca-certificates.crt
 
cat ~/.npmrc
registry=https://<your-nexus/repository/npm-group>
prefer-offline=true
cache=/home/<user>/npm_cacache/.npm
loglevel=verbose
# test
    - npm info lodash
    - npm info glob
    - npm info @types/glob@7.2.0
 
# Find and replace "npm install" by "npm ci" ???
# sed -i '/\$(MAKE) -C \$(DEVKIT) download; \\/i \\tsed -i '\''s/npm install/npm ci/g'\'' \$(DEVKIT)/package/feeds/feed_prpl/prpl-webui-bcm/Makefile;\\' altsdk/openwrt/prplos.mk
npm ci
#or
npm install

 

Bonus information:

npm ci --cache .npm --prefer-offline
  • npm ci: Performs a clean, reproducible install of dependencies exactly as locked in package-lock.json. It removes node_modules and installs fresh, ensuring consistency. Ideal for CI/CD pipelines or production builds

  • --cache .npm: Uses the .npm folder in your current directory as the npm cache location instead of the default global cache. This is useful for caching dependencies locally in your project or CI environment, speeding up installs on repeated runs.

  • --prefer-offline: Tells npm to use cached packages and metadata if available, minimizing network requests. If a package is missing from cache, npm will still fetch it from the registry. This balances speed and reliability when network access is limited or slow

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments