special sponsors

Installing node in your Lando PHP service

Written by: Team Lando Team Lando
Guide Tested: Yes

Some frontend tooling kits like Emulsify or Pattern Lab may assume that composer/php can invoke yarn/npm/node and vice-versa. This pattern, sadly, is fundamentally at odds with Lando's one-thing-per-container model.

You can, however, get around it by installing the needed dependencies directly in the service that requires them.

We've found installing node inside a Lando PHP service to generally be the path of least resistance.

1. Using build steps

Below is an example that installs node12 using build-steps.

services:
  myservice:
    type: php
    build_as_root:
      # Note that you will want to use the script for the major version of node you want to install
      # See: https://github.com/nodesource/distributions/blob/master/README.md#installation-instructions
      - curl -sL https://deb.nodesource.com/setup_12.x | bash -
      - apt-get install -y nodejs
tooling:
  node:
    service: myservice
  npm:
    service: myservice

You can verify with:

lando node -v
lando npm -v

2. Extending a Dockerfile

If you are planning to extend your service with additional build steps or would like to cache the build steps for a faster lando rebuild you should instead consider extending with a Dockerfile as in the example below:

.lando.yml

services:
  myservice:
    type: php:custom
    via: cli
    overrides:
      image: lando/php:7.4-with-node12
      build:
        context: ./
        dockerfile: Dockerfile.node
tooling:
  node:
    service: myservice
  npm:
    service: myservice

Dockerfile.node

FROM devwithlando/php:7.4-apache-2

# Choose the major node version
ENV NODE_VERSION=12

# Install node
RUN curl -sL "https://deb.nodesource.com/setup_${NODE_VERSION}.x" | bash - \
  && apt-get install -y nodejs

Again, you can verify with:

lando node -v
lando npm -v

FULL CODE EXAMPLE!

If you are interested in checking out the fully-armed and operational source code for this guide then check out this repo here.

Have a Lando question you'd like to see as a guide? Suggest one!
Want to write a Lando guide? Learn how!