Question

npm prepare on DO App Platform

Posted January 17, 2021 1.4k views
Building on DigitalOcean

My app depends on private npm packages hosted on GitHub Packages, so I have a script which generates my .npmrc before npm install.

package.json excerpt:

{
  "scripts": {
    "prepare": "npm run npmrc:create",
    "npmrc:create": "node scripts/create-npmrc"
  }
}

Here’s what npm install looks like locally:

$ npm i

> @{{org}}/{{app}}@0.0.0 prepare
> npm run npmrc:create


> @{{org}}/{{app}}@0.0.0 npmrc:create
> node scripts/create-npmrc


up to date, audited 1124 packages in 2s

106 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

Here’s my build log:

=> Initializing build
=> Retrieving source code to /workspace
=> Checking out commit "{{hash}}"
=> Validating environment

=> Building app using buildpacks
=> Injecting app environment variables:
     GITHUB_TOKEN
=> Configuring custom build command to be run at the end of the build:
     npm run build
=> Running buildpack detection

heroku/nodejs-engine  0.5.0
digitalocean/node     0.2.0
digitalocean/procfile 0.0.3
digitalocean/custom   0.1.0


=> Building app

---> Node.js Buildpack
---> Installing toolbox
---> - jq
---> - yj
---> Getting Node version
---> Resolving Node version
---> Downloading and extracting Node v12.20.1
---> Parsing package.json
---> No file to start server
---> either use 'docker run' to start container or add index.js or server.js
Installing node_modules using npm (from package-lock.json)
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/@{{org}}%2f{{lib}} - Not found
npm ERR! 404
npm ERR! 404  '@{{org}}/{{lib}}@0.1.1' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/apps/.npm/_logs/2021-01-16T22_53_11_554Z-debug.log
unable to invoke layer creator
installing node_modules: exit status 1
ERROR: failed to build: exit status 1
 ! Build failed (145)

It looks like prepare isn’t run when App Platform builds my app.

Related: https://www.digitalocean.com/community/questions/how-to-use-private-npm-packages-on-do-app-platform

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.

×
Submit an Answer
1 answer

👋🏼 @ptrkcsk

App Platform runs npm ci for npm versions 5.7.0 and above, and npm install for older versions. Looking through similar issues reported by NPM users, it seems like npm ci runs prepare/preinstall scripts differently than npm install especially around the ordering of these scripts and the registry auth checks.

As a workaround I would recommend committing an .npmrc file to your repo. You can store secrets or tokens in environment variables and reference them in .npmrc so you wouldn’t need to commit them to your repo. See: https://docs.npmjs.com/using-private-packages-in-a-ci-cd-workflow#create-and-check-in-a-project-specific-npmrc-file

Let me know if that helps!