ddev: `ddev exec` doesn't pick up new nvm-defined node version

Is there an existing issue for this?

  • I have searched the existing issues

Run a Diagnostic and Paste Link Here

No response

Expected Behavior

Through a post-start hook I want to call yarn start, but this fails because it does not meet the required node version defined in package.json of a project. Switching the web container to use the required node version works, but yarn start does not seem pickup this and instead continues to use the nodejs_version set in config.yaml.

Actual Behavior

I am trying to use a different version of node in the web container and setup a post-start hook for yarn start. The version provisioned is 14 however a project needs a lower version “10.16”. I setup a post-start hook command like so:

hooks:
  post-start:
    - exec-host: ddev nvm install 10.16.0 && ddev nvm use
    - exec: yarn start

exec-host is needed for the nvm commands as otherwise it fails.

This outputs:

v10.16.0 is already installed.
Now using node v10.16.0 (npm v6.9.0)
Found '/var/www/html/.nvmrc' with version <10.16.0>
Now using node v10.16.0 (npm v6.9.0)
yarn run v1.22.19
error example-project@1.0.0: The engine "node" is incompatible with this module. Expected version "^10.16.0". Got "14.21.1"
error Commands cannot run with an incompatible environment.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

The node --version command returns the original node version provisioned in config.yaml 14.21.1 when run via a hook, however if I ddev ssh and then run node --version it is set to v10.16.0 and yarn runs fine.

Steps To Reproduce

No response

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 27 (26 by maintainers)

Commits related to this issue

Most upvoted comments

FYI there is a side effect of modifying PATH with nvm

https://github.com/nvm-sh/nvm/issues/2914 https://github.com/nvm-sh/nvm/issues/2968

ddev nvm install xxxx

Can trigger a bash error in console:

v10.16.0 is already installed.
Now using node v10.16.0 (npm v6.9.0)
bash: [: -ne: unary operator expected
bash: [: -ne: unary operator expected
bash: [: -ne: unary operator expected

It does still work in the context of ddev though and is a nvm bug, which will hopefully be fixed in a new release or ddev exec provides the PATH natively in the future.

It occurs when trying to install a version you already have, which if you use a post hook, is going to be repeated each time. If you really care, you can do a condition to check the node version first and if it matches what is required don’t run nvm install xxxx.

I read the issues, and it is in fact an nvm bug, and is apparently fixed in the current latest nvm version, so should show up fixed in DDEV v1.21.5, which will be in a couple of weeks.

Prerelease fixing this issue is available https://github.com/drud/ddev/releases/tag/v1.21.5-alpha1

Thank you! For reference for anyone else coming across this issue, adding the nvm version to the PATH followed by whatever command needed works, in my case yarn start

hooks:
  post-start:
    - exec-host: ddev nvm install 10.16.0 && ddev nvm use
    - exec-host: ddev exec 'export PATH=~/.nvm/versions/node/v10.16.0/bin:$PATH; yarn start'

I can call nvm use because there is a .nvmrc in this project which has the value of 10.16.0.

It must be run with the ddev exec command that modifies the PATH, otherwise ddev exec will fail given it cannot see the node version due to the PATH problem.

Alternatively, you can ddev ssh and then run whatever commands as the node environment will be correct, this however isn’t really ideal if you want to automate things with hooks like I do.