Git Product home page Git Product logo

npm-g_nosudo's Introduction

npm-g_nosudo

A shell script which will fix the problem where you want to stop using sudo for npm -g on Ubuntu.

Inspired by a day trying to sort various machines out on our system to run code nicely.

and this Stackoverflow answer

Tested on:

  • Ubuntu 14.04 with Bash
  • Fedora 30 with ZSH

Usage:

Download the script, run it:

./npm-g-nosudo.sh

or

wget -O- https://raw.githubusercontent.com/glenpike/npm-g_nosudo/master/npm-g-nosudo.sh | sh

If you run the former command (rather than the wget version), the script will give you the option to fix your .bashrc or .zshrc file(s) automatically to use the settings from Sindre Sorhus' Guide

If you say "n", it will print the variables you need to enable you to fix manually.

If you say "y", you will need to source your corresponding file for your current environment vars to be updated.

If you run the command via wget, this changes the stdin for the script, so it doesn't run interactively and won't update your file. It will echo out the variables you need to set near the end of the script output so you can copy these and add this to your environment manually.

Important

After updating your environment files, you will need to source the corresponding file before your npm binaries will be found in the current terminal session, e.g. for bash:

source ~/.bashrc

or just open an new terminal session.

The script may not play nicely with Node Version Manager so it will not run if if finds the script installed.

License

MIT © Glen Pike

npm-g_nosudo's People

Contributors

alexfornuto avatar anteriovieira avatar austinbutler avatar bryant1410 avatar diokey avatar glenpike avatar javadoug avatar jimvanderveen avatar nshtg avatar pgilad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

npm-g_nosudo's Issues

Yeoman Doctor problem - NODE_PATH matches the npm root

After that spell:

wget -O- https://raw.githubusercontent.com/glenpike/npm-g_nosudo/master/npm-g-nosudo.sh | sh
source ~/.bashrc

I get

✔ Global configuration file is valid
✔ Node.js version
✔ No .bowerrc file in home directory
✔ No .yo-rc.json file in home directory
✔ npm version
✖ NODE_PATH matches the npm root
npm global root value is not in your NODE_PATH

[Info]
  NODE_PATH = /usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript
  npm root  = /home/vagrant/.npm-packages/lib/node_modules

[Fix] Append the npm root value to your NODE_PATH variable
Add this line to your .bashrc
    export NODE_PATH=$NODE_PATH:/home/vagrant/.npm-packages/lib/node_modules
  Or run this command
    echo "export NODE_PATH=$NODE_PATH:/home/vagrant/.npm-packages/lib/node_modules" >> ~/.bashrc && source ~/.bashrc

when I'm running yeoman install via

npm install -g grunt-cli bower yo generator-karma generator-angular

When I'm doing nosudo manually (via https://github.com/sindresorhus/guides/blob/master/npm-global-without-sudo.md), everything is ok.

$ node -v; npm -v
v4.2.2
3.4.0

How do I reverse the changes of this script?

I tried using this script, both downloading it/running it, and using the wget method, but i still get EACCESS errors when trying to install packages globally. To make things even worse, with whatever this script did, I can now no longer use sudo npm commands, it returns error: /usr/bin/env: node: No such file or directorythe only way for me to run sudo npm commands now is to use an elevated terminal.

No npm-cli command excuted

After wget the sh file, no npm-cli command excuted.
Such as "yo" command shows "yo: command not found"

Script name misspelled in README.md

The instructions for running the script in README.md say

Download the script, run it:
./npm-g-nosudo.sh

However, the actual script name is npm-g-no-sudo.sh.

sh: 74: [: UG}: unexpected operator

When running this with wget command on Raspbian I get the following errors:

sh: 74: [: UG}: unexpected operator
sh: 77: [: UG}: unexpected operator
sh: 88: syntax error: Missing '}'

Yo still not found

Hi,
I did run the file and followed the instructions on Mac OS X yosemite.
I added

source ~/.bashrc

on the first line of my .bash_profile
and all the path informations were set - but still the yo command is unknown (terminal).

After hours of trying and searching the net my question is: what do I have to do to get the things "undone"? I'm pretty new to Mac / unix systems so I'm not sure if I can just delete the added folders (.npm-packages and .npm) and remove the PATH informations that came with the script.

Any help is welcome!

Best regards
Luckyfella

Script fails and nukes system-level npm install

I tried running the following from the README:

$ wget -O- https://raw.githubusercontent.com/glenpike/npm-g_nosudo/master/npm-g-nosudo.sh | sh

It failed with this error:

--2022-02-05 00:50:21--  https://raw.githubusercontent.com/glenpike/npm-g_nosudo/master/npm-g-nosudo.sh
SSL_INIT
Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 2606:50c0:8003::154, 2606:50c0:8000::154, 2606:50c0:8001::154, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|2606:50c0:8003::154|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3961 (3.9K) [text/plain]
Saving to: ‘STDOUT’

     0K ...                                                   100% 3.32M=0.001s

2022-02-05 00:50:21 (3.32 MB/s) - written to stdout [3961/3961]

Error: Cannot find module 'nopt'
Require stack:
- /usr/lib/node_modules/npm/node_modules/@npmcli/config/lib/index.js
- /usr/lib/node_modules/npm/lib/npm.js
- /usr/lib/node_modules/npm/lib/cli.js
- /usr/lib/node_modules/npm/bin/npm-cli.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/usr/lib/node_modules/npm/node_modules/@npmcli/config/lib/index.js:4:14)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
Error: Cannot find module 'nopt'
Require stack:
- /usr/lib/node_modules/npm/node_modules/@npmcli/config/lib/index.js
- /usr/lib/node_modules/npm/lib/npm.js
- /usr/lib/node_modules/npm/lib/cli.js
- /usr/lib/node_modules/npm/bin/npm-cli.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/usr/lib/node_modules/npm/node_modules/@npmcli/config/lib/index.js:4:14)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)


sh: line 151: syntax error near unexpected token `)'
sh: line 151: `    [Yy]* ) fix_env;;'

I also tried it with bash and not sh, and got the same result.

After running the command from the README, my npm install was nuked:

$ npm

node:internal/modules/cjs/loader:933
  const err = new Error(message);
              ^

Error: Cannot find module 'semver'
Require stack:
- /usr/lib/node_modules/npm/lib/utils/unsupported.js
- /usr/lib/node_modules/npm/lib/cli.js
- /usr/lib/node_modules/npm/bin/npm-cli.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/usr/lib/node_modules/npm/lib/utils/unsupported.js:2:16)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:999:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/usr/lib/node_modules/npm/lib/utils/unsupported.js',
    '/usr/lib/node_modules/npm/lib/cli.js',
    '/usr/lib/node_modules/npm/bin/npm-cli.js'
  ]
}

Node.js v17.3.0

I had to reinstall npm, Node and some related packages in order to get a working system npm install again:

$ sudo pacman -S npm nodejs semver node-gyp nodejs-nopt

I'm on Manjaro with npm v8.4.0, Node v17.3.0, Bash v5.1.16 and wget v1.21.2.

Still getting permission errors.

So after running the script and sourcing .bashrc I still get the following error:

npm ERR! Linux 3.19.0-31-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install" "yo" "-g"
npm ERR! node v4.2.1
npm ERR! npm  v2.14.7
npm ERR! path /home/otis/.npm-packages/lib
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall mkdir

npm ERR! Error: EACCES: permission denied, mkdir '/home/otis/.npm-packages/lib'
npm ERR!     at Error (native)
npm ERR!  { [Error: EACCES: permission denied, mkdir '/home/otis/.npm-packages/lib']
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/home/otis/.npm-packages/lib' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/otis/npm-debug.log

I was able to fix by running sudo chown -R $(whoami) ~/.npm-packages but is there a reason the user didn't already own this directory???

Don't mind the fix I can add it to my script just wondered if there are any draw backs / uh-ohs to doing this?

syntax error near unexpected token - line 142

Terminal Exit:
sh: line 142: syntax error near unexpected token )' sh: line 142: [Yy]* ) fix_env;;'

I'm on a
Macbook Pro 13", mid 2012
2,5 GHz Intel Core i5
10GB 1600 MHz DDR3
Intel HS Graphics 4000 1536 MB
With:
Mac OS El Capitan (10.11.5)

Thank you,
Hélio

Yo still not found

HI, I download the script with

wget -O- https://raw.githubusercontent.com/glenpike/npm-g_nosudo/master/npm-g-nosudo.sh | sh

and all work fine, but when I install yo and try the "yo" in my terminal I get:

yo: command not found

I Run: npm -g list -depth=0
and get:

Using elementary OS (based ubuntu 14.04) 32 bit.

nvm is not compatible with the npm config "prefix

After I ran ./npm-g-nosudo.sh

$ source ~/.bashrc
nvm is not compatible with the npm config "prefix" option: currently set to "/Users/eugenekim/.npm-packages"
Run `npm config delete prefix` or `nvm use --delete-prefix v7.4.0 --silent` to unset it.

Warn the user when neither .bashrc nor .zshrc is found.

Currently, even when using bash, if .bashrc is not created yet, this script does not change the PATH permanently. It should either:

  • complain that it's missing and tell the user to create it first, or
  • tell the user which lines to add, or
  • try to use another way to detect if bash is being used, and create the appropriate file, or
  • ask the user which shell they are using, or
  • ???

fixenv script needs to export the env vars

After running the script, my -g modules were "not found" on the command line. I modified my .bashrc file to change the generated entries to export the env vars. I suspect you will want to change (around line 115)[https://github.com/glenpike/npm-g_nosudo/blob/master/npm-g-nosudo.sh#L115] to export the env vars so that the resolve in the terminal.

Fix .zshrc automagically

It would be nice if the script could automagically detect a ~/.zshrc file and fix that as well.

Include the source command

I tried the script 2 times.

The first time I just launched it and ignored the output, so I didn't notice this tip:

Don't forget to run 'source ~/.bashrc'

If you do forget, then you'll lose commands installed via npm.

Since the purpose is to automate this procedure, I think that additional step should be included in the script.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.