简体   繁体   中英

How to use npm in an environment with restricted internet access

In a restricted environment where not every user has access I would like to be able to use npm offline where ever possible.

My idea is to point the global config at a shared cache directory so that power users can do installs and the dependencies will end up in the cache directory. Other users can then do npm offline installs for anything previously in the cache.

So 2 Questions:

Will this work? Short of setting up my own local npm repository is there an easier way?

Per documentation:

npm install (with no args in a package dir)
npm install <tarball file>
npm install <tarball url>
npm install <folder>
npm install <name> [--save|--save-dev|--save-optional]
npm install <name>@<tag>
npm install <name>@<version>
npm install <name>@<version range>
npm i (with any of the previous argument usage)

As such, npm allows you to do:

npm install /path/to/folder/containing/ node_modules

For example: npm install ~/Downloads/http-proxy , provided that the node_modules folder resides within http-proxy .

You could set your repository up on an internal (accessible) server and direct people to download by the same name from there.

Thanks for the answers. What I've ended up doing is using https://github.com/rlidwka/sinopia

This acts as a mirror repository. I can give this process internet access and not other users. Then I set a environment variable for all users to point their npm repository at the sinopia instance.

Early days but this seem to be working well.

r3mus is right. Though, for each user it would result in some cognitive overhead and possibly management issues.

What might work better is to have a corporate hosted npm repository (as described here: http://clock.co.uk/tech-blogs/how-to-create-a-private-npmjs-repository ) and then just have the users change (once) their registry settings via npm set registry http://yourregistry.com

For build servers, a reasonable strategy is to symlink the node_modules directory against an existing directory where the modules have already been installed.

eg my powershell script might read something like this

If (-Not (Test-Path node_modules)) 
{
    & cmd /c mklink /d /j node_modules D:\npm-cache\node_modules
    Write-Verbose "Symlinked node_modules"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM