So I've got the following code that should check whether an npm package is installed:

def is_installed(name, as_global=True):
    # Returns whether NPM package is installed. 
    s = shell(_get_npm_command("ls -p --depth 0", as_global))
    match = re.search(r'\b%s\b' % (name), s['stdout'])
    if match:
        return True
    return False

With the shell function being a subprocess.Popen() wrapper:

def shell(c, stdin=None, env={}):
    # Simplified wrapper for shell calls to subprocess.Popen()
    environ = os.environ
    environ["LC_ALL"] = "C"
    for x in env:
        environ[x] = env[x]
    if not "HOME" in environ:
        environ["HOME"] = "/root"
    p = subprocess.Popen(shlex.split(c),
    data = p.communicate(stdin)
    return {"code": p.returncode, "stdout": data[0],
        "stderr": data[1]}

and with a get_npm_command like this:

def _get_npm_command(command, as_global, path=None):
    # returns npm command
    if as_global:
        return _get_global_npm_command(command)
        return _get_local_npm_command(command, path)

def _get_global_npm_command(command):
    # returns global npm command
    return "gksu -u npm 'npm " + command + " -g'"

def _get_local_npm_command(command, install_path):
    # returns local npm command
    if install_path:
    return "npm " + command + " "

As you can see, the eventual command is:

gksu -u npm npm ls -p --depth 0

In other words, I've created a user called npm to install global npm packages, because not that long ago npm recommended that one should not use root privileges in order to use it. They've changed their minds, but I intend stick with this solution I've found on the web that someone came up with.

Anyway, gksu prompts me with a password for this command, but npm is not meant to have a password and so I would like to be able to run this command without being asked a password for.

I've tried the following:


npm     ALL=(ALL) NOPASSWD: /usr/bin/npm

..but that doesn't seem to have any effect.

  • I don't know if it'll solve your problem but you'd need to call sudo gksu and adjust the sudoers rule to be gksu at the end instead of npm – Jeff Schaller Jun 13 '16 at 3:05
  • You mean "/etc/sudoers.d/arkos.gksu" and "npm ALL=(ALL) NOPASSWD: gksu"? That has no effect. Even "npm ALL=(ALL) NOPASSWD: ALL" has no effect. – Folaht Jun 14 '16 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.