Wednesday, June 13, 2018

[ProGet] Setup NPM registry and publish package


  ProGet    NPM Package  





▌Introduction


We are going to create a NPM registry by ProGet and learn how to publish and install packages from it.



▌Environment


▋ProGet 5.0.11
▋Windows Server 2008 R2
▋NPM 6.1.0



▌Setup PorGet’s NPM registry


▋Create new feed

Go to Feeds and click 【Create New Feed】, then create one Feed for NPM.






After the Feed is created, take a look at it and copy the API endpoint URL:


In this example, it is “http/myserver/npm/mynpm/”, which is also the URL of our NPM registry.





▋Set scoped registry for npm

Register a scoped registry like this to .npmrc

$ npm config set @myscope:registry http://myserver/npm/feedName/

Scope name can be your unique name or organization name.
I will register a registry which has a scope named “JB”.

Open .npmrc by

$ npm config edit


You will see the scoped registry is appended to the file.





▋Set ProGet’s login user for the registry

Now close the .npmrc. We will add the permission for it with ProGet’s default user: “Admin”.

$ npm adduser --registry=http://myserver/npm/feedName/ --scope=@myscope


Don’t miss the last slash “/” in the URL of registry!
I encountered the following problem just because of it, and was stuck for 2 hours. lol


npm add user, will fail with error:
npm ERR! 404 Registry returned 404 for PUT on http://myserver/npm/-/user/org.couchdb.user:admin



If everything is cool, you will get the following logined message.



Now open .npmrc again to check the updated information for your registry.




▌Upload or Publish NPM package


▋Prepare your awesome package

Since we use a scoped registry, it’s recommend to name you package with the mapping scope.
You can name the package with a scope name by

$ npm init --scope=@myscope

Which will make the package’s name in package.json looked like this,

{
  "name": "@myscope/awesome-pkg",
}




▋Upload thru ProGet

We can zip the package and upload it through PorGet’s website.
Notice that the supported file must be zipped as tar.gz.


▋Zip your awesome package

I use 7.zip for example. First zip the folder as .tar




Then zip the .tar as .tar.gz




▋Upload

Go to ProGet’s Feed page, and click on 【Add Package】



Choose 【Upload npm Package】






If you get the below error message, check the zipped format.




▋Publish thru npm

A better way is using npm cli.

▋Update package.json

Add the following publish config to package.json, in order to force publishing to our private registry. (See reference)

"publishConfig":{"registry":"http://myserver/npm/feedName/"}


▋Add .npmignore (Optional)

If you wanna to skip some folders or files while publishing the package.
Add the .npmignore in root path to ignore them. (See reference)


▋Publish

$ npm publish




▌Install NPM package


Congrates! We finally successfully upload our own awesome package.
Now it’s easy to install it thru npm.



Enjoy it!


▌Reference






No comments:

Post a Comment