Skip to content

I would like to create a feature that adds some npm packages, yet when I try that I get npm not found #763

Description

@hpe-ykoehler

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions