I would like to create a feature that adds some npm packages, yet when I try that I get npm not found
I am trying to create a local/private feature, I used to run those npm install as part of the postCreateCommand, but I need to create a standalone image and would like to use devcontainer to do so.
Since the image will be instatiated from docker run, it will not trigger post-create scripts, as such I was to install some npm package within the image before it gets created.
My feature is using InstallAfter common-utils and node, it then run npm as su -c ${USERNAME} and yet npm is not found, I tried to use the hard-coded path directly still the same issue.
{
"id": "myfeature",
"version": "0.0.1",
"name": "MyFeature",
"installAfter": [
"ghcr.io/devcontainers/features/common-utils",
"ghcr.io/devcontainers/features/node"
]
}
my install script
#!/bin/bash
USERNAME="${USERNAME:-"${_REMOTE_USER:-"automatic"}"}"
set -e
if [ "$(id -u)" -ne 0 ]; then
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
exit 1
fi
# Ensure that login shells get the correct path if the user updated the PATH using ENV.
rm -f /etc/profile.d/00-restore-env.sh
echo "export PATH=${PATH//$(sh -lc 'echo $PATH')/\$PATH}" > /etc/profile.d/00-restore-env.sh
chmod +x /etc/profile.d/00-restore-env.sh
# Determine the appropriate non-root user
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
USERNAME=""
POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
for CURRENT_USER in "${POSSIBLE_USERS[@]}"; do
if id -u ${CURRENT_USER} > /dev/null 2>&1; then
USERNAME=${CURRENT_USER}
break
fi
done
if [ "${USERNAME}" = "" ]; then
USERNAME=root
fi
elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then
USERNAME=root
fi
su ${USERNAME} -c "/usr/local/share/nvm/versions/node/v20.9.0/bin/npm install -g somepublicnpmpackages" 2>&1
if I run my image using docker run I can find that the npm program is located at /usr/local/share/nvm/versions/node/v20.9.0/bin/ so I tried hard-coded that path but somehow that didn't work.
I would appreciate any guidance here
Hi 👋
Looks like you are using the Node Feature to install node? Then, I think you need to run the below 👇 command before running npm install.
su ${USERNAME} -c "umask 0002 && . '/usr/local/share/nvm/nvm.sh' && nvm use default"
Reasoning: If you're trying to run an npm command and you're using NVM to manage your Node.js versions (used by the Node Feature), you might need to run this ^ command first to ensure that the correct Node.js and npm versions are being used. I think that's correctly set when you are within a dev container, but I wonder if it's causing issues with local Features.
@hpe-ykoehler If this ^ still doesn't fix your issue, then can you point me to sample repo where I can reproduce this issue? This will help in faster investigations, thanks!
Another pointer, would be worth checking if USERNAME is computed/passed correctly.
It seems reasonable as a requirement, so I tried to that but the nvm.sh is not accessible when my feature install itself, even if I set the install-after, it looks as if each feature do not have access to file installed by previous feature.
[3 ms] @devcontainers/cli 0.52.1. Node.js v18.18.2. linux 5.15.0-86-generic x64.
[1219 ms] Resolving Feature dependencies for 'ghcr.io/devcontainers/features/common-utils:2'...
[1867 ms] Resolving Feature dependencies for 'ghcr.io/devcontainers/features/node:1'...
[2450 ms] Resolving Feature dependencies for 'ghcr.io/devcontainers/features/docker-in-docker:2'...
[3093 ms] Resolving Feature dependencies for './features/myfeature'...
...
0.149 ===========================================================================
0.149 Feature : myfeature build requirements
0.149 Description :
0.149 Id : Unknown
0.149 Version : 2.0.1
0.149 Documentation :
0.149 Options :
0.149 USERNAME="myuser"
0.149 ===========================================================================
0.242 bash: /usr/local/share/nvm/nvm.sh: No such file or directory
0.244 ERROR: Feature "myfeature build requirements" (Unknown) failed to install!
Such an error doesn't appear to be USERNAME related, I disabled my feature then jump into the container and I can see that the /usr/local/share/nvm/nvm.sh is present and exists, but apparently feature aren't built on top of other feature layer, which seems quite limiting.
I will create a "local" node feature and try to install npm package via an parameters as a workaround for now, assuming that this would work.
Ok, copying the public node js feature into a local one, then adding the above + npm install command does work.
I would have preferred installing additional package as an extra feature, but at the moment I could alter the node feature to accept a string argument such as NPM_ADDITIONAL_PACKAGES and have a npm install step at the end that would install those.