1

I've set up a Travis CI to run a few scripts that should:

  1. Deploy some static pages to Github pages
  2. Deploy an NPM package to npmjs

Item 1 works, Item 2 doesn't.

Here's what my travis.yml file looks like:

language: node_js
node_js:
 - '10'
script:
 - gulp build
 - gulp npmDist
deploy:
 - provider: pages
    local_dir: dist-site/
    skip_cleanup: true
    github_token: "$GITHUB_TOKEN"
    on:
      branch: master
 - provider: npm
    email: myemail@mydomain.com
    api_key:
      secure: THE-API-KEY-I-GOT-BY-CREATING-A-TOKEN-ON-NPMJS-AND-ENCRYPTING-IT-USING-TRAVIS-ENCRYPT-COMMAND-IN-TERMINAL
    on:
      tags: true
      repo: githubaccount/reponame
      all_branches: true

I trigger the script in two ways: - When I merge to master, it deploys to GitHub Pages. - When I create a tag and push to master it should deploy the package to npmjs.

As stated, the first part of the file works, as it actually deploys to GitHub Pages.

Here's the error I get from npmjs:

npm ERR! publish Failed PUT 401
npm ERR! code E401
npm ERR! You must be logged in to publish packages. : package-name

(oh, and a strange thing: Travis returns with a "Build Passed" and the succesful status (green), even though there's obviously something wrong)

Hope this makes sense? Thanx in advance for any help.

kimblim
  • 76
  • 4

2 Answers2

1

Fixed it — instead of having this in the travis.yml-file:

api_key:
      secure: THE-API-KEY-I-GOT-BY-CREATING-A-TOKEN-ON-NPMJS-AND-ENCRYPTING-IT-USING-TRAVIS-ENCRYPT-COMMAND-IN-TERMINAL

I changed it to:

api_key: "$NPM_TOKEN"

..and added the NPM Token as an environment variable inside the Travis CI dashboard.

(Still curious as to why it didn't work, but I can't be bothered to do something about, as I've already wasted way too much time on this issue today)

kimblim
  • 76
  • 4
0

I had the same problem and I just removed all previous keys and generated them again and my code looks like this:


deploy:
  provider: npm
  email: $NPM_USER
  api_key: $NPM_TOKEN

To create your NPM_TOKEN you must:

  1. Go to your npm profile
  2. Tokens
  3. Create Token
  4. Select "Read and Publish" and create it.

Then you can specify it in your env variables for the corresponding project. The key do not have to be encrypted and the user is your email. That will be it.

You will receive a notification like:

Installing deploy dependencies
dpl.2
Preparing deploy
dpl.3
Deploying application
+ your-artifact@x.x.x
EliuX
  • 11,389
  • 6
  • 45
  • 40