
Fixing npm permissions
Fixing npm permissions
null
Content
0 -> In this video, I'm going to show you how to fix npm's permissions
3.995 -> so that you can install npm packages globally without using sudo.
8.174 -> If your permissions are not set up properly
11.054 -> you'll get this EACCES error when you try to install things globally.
15.596 -> I'm going to show you a few different ways you can fix this,
18.676 -> and you'll want to watch this video all the way through
21.24 -> before moving ahead
22.6 -> just to see which of these ways is best for you.
24.895 -> It might also be a good idea to backup before you start.
28.84 -> So one way to fix this error
30.34 -> is to make it possible for the user that you're using
32.909 -> to write to the correct directory
35.014 -> For those who are new to file permissioning, I'm just going to introduce the concept really quick.
39.173 -> To see all the files in a directory
41.42 -> I can use the ls command
43.22 -> and if I use the -l flag
45.12 -> that will show me the permissions.
47.36 -> So I have a file named file.txt
50.14 -> and the user that owns this is the root user.
53.578 -> To the left of this, I can see who has what permissions on this file.
57.978 -> The first three letters indicate what the owner can do with this file
62.275 -> so root can read and write this file.
64.675 -> Now lets check to see what user we're using with the whoami command.
68.574 -> So the name of the user that I'm currently running as
71.54 -> is your_user
73.86 -> So this means Ican't edit file.txt, it's not writable to me
77.46 -> because Idon't have write access. I'm not the owner and only the owner has write access on this.
83.721 -> But I can change the ownership of this file
86.2 -> so that I am the owner and then I would have write access
89.08 -> To do this
90.14 -> I just type sudo chown (for change owner)
93.84 -> and then my username and the file name.
97.114 -> So now when we run ls -a, you'll se that my user is now the owner of the file.
102.656 -> So what we need to do is change ownership of the directory
106.46 -> that npm writes to
108.18 -> to be the user that we're running as
110.974 -> The default directory that npm writes to is /usr/local
114.717 -> and you can find this out using npm config get prefix.
118.237 -> I'm going to look at the permissions specifically of the node_modules directory in there
123.234 -> but we'll be changing the permissions on that parent directory.
125.94 -> I'm going to use an -a flag
128.06 -> to show all hidden files, including the permissions on this directoryand its parent.
133.393 -> So you'll see that the root user owns all of these
136.593 -> and we'll want to change this to the user that you're running as.
139.674 -> All we need to do
141.889 -> is use sudo chown -R
145.599 -> to run it recursively, which means
all of the directories inside of it as well,
149.459 -> and then you put your username and then
the directory which is /usr/local/
156.13 -> Now some of you might be concerned about
158.59 -> changing the the ownership of every single
161.69 -> directory inside of /usr/local/.
You can usually get away
166.39 -> with just changing three directories.
Those are the /usr/local/lib/node_modules directory,
170.06 -> the /usr/local/bin directory,
173.98 -> and the /usr/local/share directory.
178.55 -> There are some cases where you might not
want to change permissions on any of
182.06 -> these directories at all.
183.27 -> For example, if you are using a machine
that you're sharing with
186.85 -> people who have other users on the same
machine. In this case
190.59 -> you can configure the directory to be
something different than /usr/local/.
194.05 -> To show you this I'm going to create an npm-global directory inside of my user's
199.05 -> directory, and I'm going to cd into that directory
202.48 -> and then use pwd to print the working
directory. Now all I need to do
207.75 -> is set my prefix to be this directory
211.02 -> and I do that with npm config set prefix.
214.62 -> When I run the npm install -g command
217.84 -> you'll see that it does download it to
the correct directory
221.36 -> and also creates a command for me in the
correct bin directory
227.14 -> Now let's try running this command.
230.7 -> It says it can't find the command and
that's because
234.209 -> we don't have this directory in our
system's $PATH.
238.68 -> This means our system doesn't know that
it should be looking for commands that are
241.76 -> in this directory.
242.76 -> We can solve this really easily. Just open up the profile that you have in
247.74 -> your user's home directory
249.6 -> and then add a line: export $PATH=
255.07 -> the directory of the bin directory
258.2 -> that is the full path of the bin directory
and then a colon
261.83 -> $PATH. It's important that you add $PATH
266.18 -> in there. What that's doing is adding the
existing system $PATH that has all of your
271.51 -> other commands in it to this $PATH, so you
still have access to all your other commands.
276.04 -> And now I need to refresh the variables so that
279.68 -> the system knows what the new $PATH is. I do this using source
283.34 -> than the name of the file
285.71 -> and now when I ran jshint you'll see that it actually
288.48 -> knows what command it is. So those are the different ways that you can fix the
292.37 -> permissions for npm.
293.92 -> you can change your ownership over /usr/local/ or the directors inside it,
298.99 -> or you can change the config prefix.
303.07 -> Now that you have your permission set up properly you can check out our channel
306.41 -> for other videos on working with npm.
Source: https://www.youtube.com/watch?v=bxvybxYFq2o