# Private NPM Module with Docker Containers

[NPM had announced support for private modules](https://www.npmjs.com/private-modules) and now its possible to have private or public NPM package which you can publish to Github, Gitlab or NPM repository.

what is NPM Package —

[**npm**  
*Edit description*www.npmjs.com](https://www.npmjs.com/ "https://www.npmjs.com/")[](https://www.npmjs.com/)

If we talk about NPM which is node package manager but now its like javascript package manager where you can publish a package private or public and your other apps can use that package.

Package is nothing but some software utility or some sdk interface, such package we can get and those feature will get added in our JavaScript application like webpack, grunt, gulp

to add these package or library if we can install simply

> npm install — save pkg-name

Can we build such javascript package and publish it, Yes we can publish such packages and others can consume these packages.

Now how to work with Private package which are limited to your organization or can be access with valid access or auth tokens.

Out point of discussion here is how to work with private packaged using Docker or Locally

If we are running application locally and we need to install some private or Organization package then we need to configure our .npmrc file

vi ~/.npmrc

always-auth=true  
//gitlab.com/api/v4/packages/npm/:\_authToken=AUTH\_TOKEN  
//gitlab.com/api/v4/projects/PROJECT\_ID/packages/npm/:\_authToken=AUTH\_TOKEN  
[@](http://twitter.com/zr "Twitter profile for @zr")my\_org:registry=[https://gitlab.com/api/v4/packages/npm/](https://gitlab.com/api/v4/packages/npm/)

Here an example for .npmrc for gitlab private repository, here we have to specify AUTH\_TOKEN which we can get from account -> settings-> access-token

This .npmrc will help us to pull private packages which are published in private scope as we have valid credentials to fetch private package

npm install —- save @my\_org/packagename

We can run npm install and it will install all public and private package as we have .npmrc which will validate package pull from private repository

Now what if we have to do same for Docker setup, In this case we have to make few arrangements Dockerfile for your Node projects

To access the private modules in NPM, we need to pass the `GIT_TOKEN` environment variable to the Docker image.

```
ENV GIT_TOKEN=token
```

The naive approach would be to add it using the `ENV,` However, **it does not work**. The variables set with `ENV` are for runtime only. so we can have setup like below mentioned Dockerfile

You can test docker build for this container by passing GIT\_TOKEN argument during build, To build the image using this image and the token, you can run Docker:

```
docker build --build-arg GIT_TOKEN=${GIT_TOKEN} .
```

Now we can update docker-compose.yml and populate GIT\_TOKEN for our build environment

docker-compose.override.yml

.npmrc in your code base can look like this which will map to container .npmrc using volume mapping

always-auth=true  
//gitlab.com/api/v4/packages/npm/:\_authToken=AUTH\_TOKEN  
//gitlab.com/api/v4/projects/PROJECT\_ID/packages/npm/:\_authToken=AUTH\_TOKEN  
[@](http://twitter.com/zr "Twitter profile for @zr")my\_org:registry=[https://gitlab.com/api/v4/packages/npm/](https://gitlab.com/api/v4/packages/npm/)
