Wednesday, June 21, 2017

Create Private NPM Server


  • There are couple of other solutions like Kappa and node-reggie, which enable you to set up a private NPM

Pros: - Sinopia is, it does not sync the public registry by default, but it will cache the packages only when downloaded for the first time

Set up and configure Sinopia

1. Inside the root directory, create a new directory named “sinopia”.
       mkdir sinopia 
       cd sinopia 

2. install sinopia globally inside that folder.
       npm install -g sinopia


3. Now navigate to http://localhost:4873 and you should see the web interface


4. Now, from a client machine where the private NPM will be accessed from, you need to set the NPM registry URL pointing to our Private NPM server and not the public registry.

  • If your server and client are same, you can run
                    npm set registry http://localhost:4873
  • And if your server and client are on different machines, you need to run
                    npm set registry http://path.to.your.server:4873

Setup User

1.By default there is no user. So we can create a user by adding the following code to config.yaml.


2.There I have selected password as admin$123 and get the sha1 encrypted password by opening a new command prompt and run
            
         node
         crypto.createHash('sha1').update('admin$123').digest('hex')


3. Now you can login by entering username and password.

4. To restrict access to the private registry, you can run.

        npm set always-auth true

5. Now, a new client who would like to access the registry would need to create an account by running.

       npm adduser --registry http://localhost:4873


6. Now you will see a new file created inside the sinopia folder as httpasswd.

7. It configured in config.yaml as follows.


Downloading packages

  • Create a new folder named privateProj and open a new terminal/prompt inside this folder and run
                  npm init
  • Next, we will install diskDB a node package
                 npm install diskdb –save


  • If you do notice your sinopia folder, you will see that there are only 2 files namely config and the htpasswd file and storage folder. In that folder it contain “.sinopia-db” file only.
  • Sinopia has fetched this package from the public repo. Now, if you see the sinopia folder, you will see


  • Now that we have diskdb installed in our privateProj and then we will publish it to our private registry.

Build & Publish a Node module

  • Now, to build a sample node module/reusable component, we will create a folder named db and a file named app.js inside the privateProj folder.
  • Update the app.js as below.

  • For that we would be publishing this app to our private registry. Run,

                    npm publish

  • You may need to update the package.json version.
  • Open config.yaml and scroll down to the packages section and add the user name to allow_publish.

  • Navigate to http://localhost:4873 you should see

  • Now, if someone wants to use your package all they need to do is run,
                 npm install privateProj –save


  • And then in your project, you can directly use

No comments:

Post a Comment