Search code examples
node.jsnpmnpm-publishverdaccio

How to create offline private registry of verdaccio


I create private npm registry with verdaccio.

I want to able to run npm install --registry="http://localhost:4873" and get all dependencies from private registry.

I need to publish all packages from my project node_modules directory.

I had to run npm publish in each package in node_module directory.(I could't find any better way.)

more of them published successfully But in some case, I encountered with the error. for example in zone.js package:

npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! [email protected]

prepublish: `tsc && gulp build` npm ERR! Exit status 2 npm ERR! npm

ERR! Failed at the [email protected] prepublish script. npm ERR! This is

probably not a problem with npm. There is likely additional logging

output above. npm WARN Local package.json exists, but node_modules

missing, did you mean to install?

or in acorn package:

[email protected] build:main C:\Users\Admin\Desktop\test ng\ng-prj\node_modules\acorn

rollup -c rollup/config.main.js

'rollup' is not recognized as an internal or external command,

operable program or batch file.

npm ERR! code ELIFECYCLE

npm ERR! errno 1

npm ERR! [email protected] build:main: `rollup -c rollup/config.main.js`

npm ERR! Exit status 1

npm ERR!

npm ERR! Failed at the [email protected] build:main script.

npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm WARN Local package.json exists, but node_modules missing, did you mean to install?

Is there a simple way of doing this?


Solution

  • here Verdaccio maintainer.

    I want to able to run npm install --registry="http://localhost:4873" and get all dependencies from private registry.

    What do you want is to have an offline registry with all your dependencies. Publish all node_modules is not practical and almost impossible.

    more of them published successfully But in some case, I encountered with the error. for example in zone.js

    That's the point, you would need to build each dependency, it just does not make sense. A regular project can easily have thousands of dependencies and sub dependencies. Not to mention you would lose the adventage of future dependencies updates.

    So, what you need is cache properly all dependencies in your storage folder.

    1. Run verdaccio $> verdaccio
    2. Be sure you are online
    3. Run npm install --registry="http://localhost:4873
    4. When the finish to install, inspect your local cache, see here how to find it. You should be able to see all the resolved dependencies in the cache.
    5. If you want a real offline experience, comment the proxy from the config file as follows
    packages:
      '@*/*':
        access: $all
        publish: $authenticated
        # proxy: npmjs
    
      '**':
        access: $all
        publish: $authenticated
        # proxy: npmjs
    

    If you comment out proxy Verdaccio won't ask for any update to the remotes, by default is npmjs, thus, no connection to external networks will be performed.

    1. Restart Verdaccio
    2. Repeat process as much time you need.

    So, here, the advantages of this approach.

    1. When yo back offline (you must comment out the proxy section again) you will allow Verdaccio to resolve wether you have new dependencies to be cached (in case you are using semver eg: lodash: ^1.5.6)
    2. You will have a real installation experience, no fear to remove node_modules and clean the npm cache as well.
    3. Storage is just a folder, so you can port it to another place (via USB or LAN)
    4. Share cache with multiple projects and node package manager tools (yarn, npm or pnpn)
    5. You don't have to publish each package in node_modules, thus see point 2).

    I hope this helps you. Furthermore, there are other practices related with offline mode, but only with yarn.