Run `npx` from PHP app
Hey ![]()
I need to execute some npx commands from a PHP (Symfony) app.
I’m already following this guide to add node/nvm to my setup (and to build some assets at the build hook).
I tried running npx commands but I get the following error: npx: not found.
So I added this step to the build hook: npm install -g npx but then got this error when building:
W: npm ERR! code EEXIST
W: npm ERR! syscall symlink
W: npm ERR! path ../lib/node_modules/npx/index.js
W: npm ERR! dest /app/.nvm/versions/node/v12.16.1/bin/npx
W: npm ERR! errno -17
W: npm ERR! EEXIST: file already exists, symlink '../lib/node_modules/npx/index.js' -> '/app/.nvm/versions/node/v12.16.1/bin/npx'
W: npm ERR! File exists: /app/.nvm/versions/node/v12.16.1/bin/npx
W: npm ERR! Remove the existing file and try again, or run npm
W: npm ERR! with --force to overwrite files recklessly.
W:
W: npm ERR! A complete log of this run can be found in:
W: npm ERR! /mnt/cache/npm/_logs/2020-10-13T07_36_03_432Z-debug.log
W: 0
E: Error building project: Step failed with status code 239.
``
E: Error: Unable to build application, aborting.
So it looks like npx is already here, but I can’t seem to be able to use it.
Anyone can help me with that?
Thanks!
Comments
Hey!
I haven’t been able to replicate your issue.
Could you tell me what version of PHP you’re using and show me your build hook?
Thanks!
Sure, here’s a portion of my YAML:
I still haven’t been able to replicate your
npxerror using a similar build hook.If we follow what happens during your build hook once you’ve installed
nvm, we have:nvm current: should display “system” at this point. The node system version is 6.17.1 which comes withnpm3.10.10.npxis not available with thisnpmversion, as it only appeared with version 5. You cannot usenpxusing the node version that comes by default.nvm install 12.16.1: you will get the specified node version, which comes withnpm6.13.4 sonpxwill now be available. Trying to installnpxafter this step will get you into aEEXISTerror asnpxcan already be found in thebinfolder of your current node version.npm run quiet-build: thequiet-buildnpm script should be able to make use ofnpxas well as everything that lives undernode_modules/.bin. When running annpmscript, the localnode_modules/.bindirectory and thenpmglobal’sbindirectory (in your case/app/.nvm/versions/node/v12.16.1/bin) are automatically added to yourPATHbynpm.If this clarification doesn’t help, let me know at which step you are trying to use
npxand how you invoke the command (e.g. in an npm script or directly in your build hook) so I can try to mimic your situation as good as possible.Hey Rudy,
I agree with the steps and how you describe them:
nvm currentdisplayssystemnvm install 12.16.1does its jobnpm run quiet-build, tooBut what I’m looking for is the ability to run
npxafterwards, when the app is built and deployed.When connecting with
platform sshand runningnpx -vI get-bash: npx: command not found(which makes sense becausenode -vdisplaysv6.17.1). Is that normal?How come Node “switched back” to its system version?
How can I use the
node/npm/npxtools installed bynvm?But! I now understand why
npxcan’t run anything, that’s because the filesystem is read-only so it can’t download packages, right? But I could theorically call it with--no-installif the packages are listed inpackages.jsonand installed, right?Current fix (but looks like a hack to me):
Replace
npx --no-installcalls withOh, if you want to be able to run
npxvia SSH directly in your container, then it’s a different story!This is normal. The different build hooks and your shell when you SSH into your container are different sessions. Therefore, environment variables set in your hooks won’t affect your environment when using SSH. Everything the
nvm.shscript does has no effect anywhere else than in the hook it is being executed. After that, your environment is unaware ofnvm's presence. Since node 12 has been installed vianvm, the only available version to your system at this point is the default one: 6.17.1.Everything is there, but you need to set the environment variables. To do so, just add these lines in your
.environmentfile:When connecting with
plaftorm ssh, you should now be able to be in a situation where:Note: you will see a message saying
npm update check failedwhen getting in your shell. This is due to an access right limitation when nvm wants to check for updates. You can ignore this. Just know that you will be runningnpmandnpx6.13.4 at this point (as opposed to 6.17.1 (as of today) in your hooks.)Yes, the filesystem is read-only so this will limit what you can do with
npxbut you don’t have to pass the--no-installoption.npxwill check for the command you want to execute in the local and globalbindirectories before trying to download anything so any already installed packages can be run by doingnpx [package]directly. Also, even with the--no-installoption, trying to run a non-installed package will result in aEROFSerror asnpxis going to try to perform write operations anyway.Please sign in to leave a comment.