Skip to content

Instantly share code, notes, and snippets.

@tasdikrahman
Created April 8, 2017 11:35
Show Gist options
  • Save tasdikrahman/6b2d123ac2886b15387b26502f673588 to your computer and use it in GitHub Desktop.
Save tasdikrahman/6b2d123ac2886b15387b26502f673588 to your computer and use it in GitHub Desktop.
By default npm packages would be installed in the root dir, change it to install the npm packages to the non-root user.

reference: [https://www.sitepoint.com/beginners-guide-node-package-manager/] (https://www.sitepoint.com/beginners-guide-node-package-manager/)

Changing the Location of Global Packages Let’s see what output npm config gives us.

$ npm config list
; cli configs
user-agent = "npm/3.6.0 node/v5.7.0 linux x64"

; node bin location = /usr/local/bin/node
; cwd = /home/sitepoint
; HOME = /home/sitepoint
; 'npm config ls -l' to show all defaults.

This gives us information about our install. For now it’s important to get the current global location.

$ npm config get prefix
/usr/local

This is the prefix we want to change, so as to install global packages in our home directory. To do that create a new directory in your home folder.

$ cd && mkdir .node_modules_global
$ npm config set prefix=$HOME/.node_modules_global

With this simple configuration change, we have altered the location to which global Node packages are installed. This also creates a .npmrc file in our home directory.

$ npm config get prefix
/home/sitepoint/.node_modules_global
$ cat .npmrc
prefix=/home/sitepoint/.node_modules_global

We still have npm installed in a location owned by root. But because we changed our global package location we can take advantage of that. We need to install npm again, but this time in the new user-owned location. This will also install the latest version of npm.

$ npm install npm --global
/home/sitepoint/.node_modules_global/bin/npm -> /home/sitepoint/.node_modules_global/lib/node_modules/npm/bin/npm-cli.js
/home/sitepoint/.node_modules_global/lib
└── [email protected]

Finally, we need to add .node_modules_global/bin to our $PATH environment variable, so that we can run global packages from the command line. Do this by appending the following line to your .profile or .bash_profile and restarting your terminal.

export PATH="$HOME/.node_modules_global/bin:$PATH"

Now our .node_modules_global/bin will be found first and the correct version of npm will be used.

$ which npm
/home/sitepoint/.node_modules_global/bin/npm
$ npm --version
3.7.5

Installing Packages in Global Mode At the moment we only have one package installed globally — that is the npm package itself. So let’s change that and install UglifyJS (a JavaScript minification tool). We use the --global flag, but this can be abbreviated to -g.

$ npm install uglify-js --global
/home/sitepoint/.node_modules_global/lib
└─┬ [email protected]
  ├── [email protected]
  ├── [email protected]
  ├── [email protected]
  └─┬ [email protected]
    ├── [email protected]
    ├─┬ [email protected]
    │ ├─┬ [email protected]
    │ │ ├─┬ [email protected]
    │ │ │ ├─┬ [email protected]
    │ │ │ │ └── [email protected]
    │ │ │ ├── [email protected]
    │ │ │ └── [email protected]
    │ │ └── [email protected]
    │ ├── [email protected]
    │ └── [email protected]
    ├─┬ [email protected]
    │ └── [email protected]
    └── [email protected]

As you can see from the output, additional packages are installed — these are uglify-js’s dependencies.

Listing Global Packages We can list the global packages we have installed with the npm list command.

$ npm list --global
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
....................
└─┬ [email protected]
  ├── [email protected]
  ├── [email protected]
  ├── [email protected]

The output however, is rather verbose. We can change that with the --depth=0 option.

$ npm list -g --depth=0
├── [email protected]
└── [email protected]

That’s better — just the packages we have installed along with their version numbers.

At this point you can parse JavaScript files in the terminal with uglifyjs. For example the following command would minify example.js into example.min.js:

$ uglifyjs example.js -o example.min.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment