NPM_TOKEN for Pages deployment doesn't work

For Workes & Pages, what is the name of the domain?

N/A

What is the error number?

404

What is the issue or error you’re encountering

Cannot install private npm packages while pages deployment

What steps have you taken to resolve the issue?

1, Create a Pages project and connect to the branch of my GitHub repository,
2, Set NPM_TOKEN with a readonly value (I’ve also tried a read-write value, got the same result) to both Production and Preview environment variables in Pages’ setting, the same as Install private packages | Cloudflare Pages docs says,
3, Try to deploy this Pages project, got error message says as following.

I can build this project successfully on my local environment, and I tried the value of the NPM_TOKEN on my local, it’s can install private npm packages, but doesn’t work on Pages’ environment.

Deployment error message:

2024-08-13T19:31:24.746066Z	npm ERR! code E404
2024-08-13T19:31:24.753903Z	npm ERR! 404 Not Found - GET https://registry.npmjs.org/@aurora-quest/universal-authentication-types/-/universal-authentication-types-0.8.6.tgz - Not found
2024-08-13T19:31:24.760581Z	npm ERR! 404 
2024-08-13T19:31:24.766327Z	npm ERR! 404  '@aurora-quest/universal-authentication-types@https://registry.npmjs.org/@aurora-quest/universal-authentication-types/-/universal-authentication-types-0.8.6.tgz' is not in this registry.
2024-08-13T19:31:24.772582Z	npm ERR! 404 
2024-08-13T19:31:24.779016Z	npm ERR! 404 Note that you can also install from a
2024-08-13T19:31:24.78531Z	npm ERR! 404 tarball, folder, http url, or git url.
2024-08-13T19:31:24.820365Z	
2024-08-13T19:31:24.826944Z	npm ERR! A complete log of this run can be found in: /opt/buildhome/.npm/_logs/2024-08-13T19_31_01_594Z-debug-0.log
2024-08-13T19:31:24.877001Z	Error: Exit with error code: 1
2024-08-13T19:31:24.881809Z	    at ChildProcess.<anonymous> (/snapshot/dist/run-build.js)
2024-08-13T19:31:24.886626Z	    at Object.onceWrapper (node:events:652:26)
2024-08-13T19:31:24.893982Z	    at ChildProcess.emit (node:events:537:28)
2024-08-13T19:31:24.898957Z	    at ChildProcess._handle.onexit (node:internal/child_process:291:12)
2024-08-13T19:31:24.902127Z	Failed: build command exited with code: 1
2024-08-13T19:31:25.877523Z	Failed: error occurred while running build command

NPM_TOKEN behaviour in Pages has always been a little weird for me.

What I’ve had work is the following:

  • Add an NPM_TOKEN secret with a token that can read your packages
  • Add an NPM_CONFIG_USERCONFIG env var with:
    /opt/buildhome/repo/.npmrc.pages
  • Commit an .npmrc.pages with this contents:
//registry.npmjs.org/:_authToken=${NPM_TOKEN}

This has then allowed me to use private packages without issue.

3 Likes

Oh man, you really saved my life, cannot thank you more!

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.