Skip to content

Skip npm install if we detect that npm is already installed. - #4946

Merged
islemaster merged 2 commits into
stagingfrom
skip-npm-install-if-already-installed
Nov 20, 2015
Merged

Skip npm install if we detect that npm is already installed.#4946
islemaster merged 2 commits into
stagingfrom
skip-npm-install-if-already-installed

Conversation

@islemaster

Copy link
Copy Markdown
Contributor

@wjordan Does this seem like an appropriate/safe change to our rake install task? Right now this step always breaks for me because I'm using nvm to manage my node/npm versions. I've been manually disabling this step when I want to install, but wondered if there's any reason not do skip it automatically in my case?

@wjordan

wjordan commented Oct 29, 2015

Copy link
Copy Markdown
Contributor

Last I checked we never used to install npm in this rake task, let me trace this backwards to make sure I understand what's going on and why this install script has drifted in this fashion, and to make sure this change is what we actually want.

Long-term I really hope to stop continuing to spend so much of our energy massaging this Rakefile across all of our various local-workstation edge-cases, and instead focus efforts on moving to a fully Chef-managed configuration. But that won't happen for a while.

@wjordan

wjordan commented Oct 29, 2015

Copy link
Copy Markdown
Contributor

It looks like the sudo apt-get install -y nodejs npm command was added by #4525, and the sudo npm install -g command by #4539 + #4553. These were added while I was out two weeks ago so I never reviewed them, and need to be corrected.

The which npm check as proposed won't allow this script to update to the correct npm version in the future, which is behavior I imagine we want to maintain here. Instead, I suggest these fixes:

  • The sudo nodejs install command was an unnecessary addition (the cdo-nodejs recipe already handles this for provisioned servers, and it's in our README.md setup instructions for local workstations), so it should be removed.
  • The sudo npm install -g command should be changed to retain compatibility with non-root node installations. It seems this was modified from RakeUtils.npm_update_g in order to be able to pin to a specific version, rather than @latest, so perhaps we can add another function to RakeUtils like:
  # Updates global npm package if it's outdated
  def self.npm_pin_g(package, version)
    # cf. http://stackoverflow.com/a/33426237/5505247
    output = `npm ll -pg --depth=0 #{package} | grep -o "@.*:" | sed 's/.$//; s/^.//'`.strip
    RakeUtils.sudo 'npm', 'install', '--quiet', '-g', "#{package}@#{version}" unless output == version
  end

Just a thought, would love to hear other suggestions.

@wjordan

wjordan commented Oct 29, 2015

Copy link
Copy Markdown
Contributor

More brainstorming/prototyping, I sketched out this replacement npm_install_g method a couple months ago to help support non-root npm installations (YMMV):

  # Installs list of global npm packages if not already installed
  def self.npm_install_g(*args)
    output = `npm list --global --parseable --long --depth=0 #{args.join ' '} 2>/dev/null`.strip
    if output.empty?
      install_args = 'npm', 'install', '--quiet', '-g', *args
      File.writable?("#{`npm config get prefix`.chomp}/lib/node_modules") ?
        system(*install_args) :
        sudo(*install_args)
    end
  end

@wjordan

wjordan commented Nov 13, 2015

Copy link
Copy Markdown
Contributor

@islemaster shall we just get this one-line fix merged in for now, putting off any more complicated changes for later? I think the current commit is fine as a workaround for now (at least until we need to update npm in the future, which won't happen very often anyway), and is definitely better than leaving as-is for any longer.

@islemaster

Copy link
Copy Markdown
Contributor Author

@wjordan I don't want this to introduce issues; it sounds like the root cause here is way, way deeper than what I'm addressing. If you're okay with this one-liner I'll happily merge it and stop messing with my local Rakefile.

islemaster added a commit that referenced this pull request Nov 20, 2015
…y-installed

Skip `npm install` if we detect that npm is already installed.
@islemaster
islemaster merged commit fbcc9a0 into staging Nov 20, 2015
@islemaster
islemaster deleted the skip-npm-install-if-already-installed branch November 20, 2015 22:21
deploy-code-org added a commit that referenced this pull request Nov 20, 2015
1a5cbc7 Merge pull request #5557 from code-dot-org/revert-revert-proxy (Bjvanminnen)
fbcc9a0 Merge pull request #4946 from code-dot-org/skip-npm-install-if-already-installed (Brad Buchanan)
3de1b51 Comment nvm workaround [ci skip] (Brad Buchanan)
703c04c Merge pull request #5462 from code-dot-org/generalize-mobile-audio-unlock (Brad Buchanan)
2d5cd5e Automatically built. (Continuous Integration)
d367880 Merge pull request #5552 from code-dot-org/delete_first_screen (Mehal Shah)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants