Skip to content

Instantly share code, notes, and snippets.

@ryuheechul
Created July 14, 2022 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryuheechul/e4c1ba86fc602f2a643560e00f1f9871 to your computer and use it in GitHub Desktop.
Save ryuheechul/e4c1ba86fc602f2a643560e00f1f9871 to your computer and use it in GitHub Desktop.
about CircleCI node image PATH issue regarding the use of NVM

Issue

After changing the image from cimg/node:17.2.0 to cimg/node:18.4.0

sudo npm resulted in sudo: npm: command not found.

tl;dr: it's because of this

$ sudo cat /etc/sudoers | grep -n env_reset
9:Defaults      env_reset

Cause

after looking at diff of these two files:

diff --git a/Dockerfile.17.5 b/Dockerfile.18.4
index e09b2c3..97836bf 100644
--- a/Dockerfile.17
+++ b/Dockerfile.18
@@ -2,22 +2,25 @@
 
 # Do not edit individual Dockerfiles manually. Instead, please make changes to the Dockerfile.template, which will be used by the build script to generate Dockerfiles.
 
-FROM cimg/base:2022.01
+FROM cimg/base:2022.06
 
 LABEL maintainer="Community & Partner Engineering Team <community-partner@circleci.com>"
 
-ENV NODE_VERSION 17.5.0
+ENV NODE_VERSION 18.4.0
+ENV NVM_DIR /home/circleci/.nvm
 
-RUN curl -L -o node.tar.xz "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.xz" && \
-	sudo tar -xJf node.tar.xz -C /usr/local --strip-components=1 && \
-	rm node.tar.xz && \
-	sudo ln -s /usr/local/bin/node /usr/local/bin/nodejs
+# Next two lines should be moved to cimg/base
+ENV BASH_ENV /home/circleci/.circlerc
+RUN touch /home/circleci/.circlerc && \
+	curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash && \
+	echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.circlerc && \
+	source ~/.circlerc && nvm install $NODE_VERSION && \
+	echo -e ". /home/circleci/.circlerc\n$( cat ~/.bashrc )" > ~/.bashrc && \
+	command -v nvm
 
-ENV PATH /home/circleci/.yarn/bin:$PATH
-ENV YARN_VERSION 1.22.17
+ENV YARN_VERSION 1.22.18
 RUN curl -L -o yarn.tar.gz "https://yarnpkg.com/downloads/${YARN_VERSION}/yarn-v${YARN_VERSION}.tar.gz" && \
 	sudo tar -xzf yarn.tar.gz -C /opt/ && \
 	rm yarn.tar.gz && \
 	sudo ln -s /opt/yarn-v${YARN_VERSION}/bin/yarn /usr/local/bin/yarn && \
 	sudo ln -s /opt/yarn-v${YARN_VERSION}/bin/yarnpkg /usr/local/bin/yarnpkg
-	
\ No newline at end of file

We can tell the main difference is the usage of nvm which resulted that the binary is not under at /usr/local/bin.

Resolution

A solution is shared at https://stackoverflow.com/a/40078875/1570165

sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/node" "/usr/local/bin/node"
sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/npm" "/usr/local/bin/npm"

And there is also https://gist.github.com/MeLlamoPablo/0abcc150c10911047fd9e5041b105c34.

So adding a step of adding symlink should result in

$ sudo which npm
/usr/local/bin/npm
@ryuheechul
Copy link
Author

filed an issue at upstream, CircleCI-Public/cimg-node#265

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment