Question

Followed all info I could find, getting 'npm: command not found'?

Just to be thorough, I’ll dump every minimal step I did to encounter this problem:

  1. I start a completely fresh serverless project:
    1. doctl serverless init --language js sandbox4
  2. I run the project to test that it works:
    1. doctl serverless deploy sandbox4
    2. it works, it’s deployed and I can see it in the web GUI. 🎉
  3. I add a single dependency, (in this case mongodb)
    1. cd sandbox4/packages/sample/hello
    2. npm init -y (generate a package.json for the function package)
    3. npm install --package-lock-only mongodb
  4. I go back to the same directory and re-run the deploy command
    1. cd ../../../..
    2. doctl serverless deploy sandbox4
  5. it fails with the following:
Deploying '/home/j/doctl/sandbox4'
  to namespace 'fn-52e42936-046c-46fb-bf57-2cd06f2ddbe2'
  on host 'https://faas-ams3-2a2df116.doserverless.co'
Started running npm install --production in /home/j/doctl/sandbox4/sample/hello
Output of failed build in /home/j/doctl/sandbox4/packages/sample/hello
/bin/bash: npm: command not found


Failures:
Error: While deploying action 'sample/hello': 'npm install' exited with code 127

I have so many questions:

  • am I using the wrong runtime? (it’s currently nodejs:default)
  • did I structure my project wrongly?

Currently it looks like this:

➜  doctl tree sandbox4 -a
sandbox4
├── .deployed
│   └── versions.json
├── .gitignore
├── packages
│   └── sample
│       └── hello
│           ├── hello.js
│           ├── package.json
│           └── package-lock.json
└── project.yml

4 directories, 6 files
➜  doctl 


and the only content that should look any different from the auto-generated sample files is the package.json (and its lock file), it looks like this:

➜  doctl cat sandbox4/packages/sample/hello/package.json 
{
  "dependencies": {
    "mongodb": "^4.7.0"
  }
}

  • can I get more verbose error output somehow? I want more information!
Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
July 5, 2022
Accepted Answer

Hi there,

I’ve just tried this out and it seems to all be working as expected.

I’ve created a demo project with the exact same steps as you:

https://github.com/bobbyiliev/serverless-hello-wolrd-node

Would you mind trying to run this and let me know if you are still seeing the issue:

  • Clone the repo:
git clone https://github.com/bobbyiliev/serverless-hello-wolrd-node.git
  • Deploy the project:
doctl serverless deploy serverless-hello-wolrd-node
  • Invoke the function:
doctl serverless functions invoke sample/hello

EDIT: I’ve just received some feedback from our FaaS team, in order for your function to be built remotely, you need to specify the --remote-build flag.

Alternatively, you need to install npm locally so that the build could succeed on your local machine too.

Let me know how it goes!

Best,

Bobby

I encountered the exact same error. Read the output message with care, and you can the deploy is trying to run npm on your local machine, and your npm is not in its $PATH envoirment.

On my machine, that was caused by install node via nvm. Thus adding a link of your npm cli to /usr/local/bin should fix this issue.

Rodric Rabbah
DigitalOcean Employee
DigitalOcean Employee badge
August 2, 2022

This comment has been deleted

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel