5

In npm, I want to be able to install a package from a private GitHub repo as a dependency through the git+https way without having to hardcode the actual github_username:personal_access_token, but rather plug them into the dependency string as (environment) variables.

So instead of

package.json:

...
"dependencies": {
  ...
  "my-private-github-repo": "git+https://<github_username>:<personal_access_token>@github.com/some/package.git",
  ...
}

I would like something like this:

package.json:

...
"dependencies": {
  ...
  "my-private-github-repo": "git+https://${github_username}:${personal_access_token}@github.com/some/package.git",
  ...
}

Hardcoding credentials is a major no-no when applying version control to package.json which I'd like to be able to do.

What is the best way to do this?

calmity
  • 113
  • 1
  • 9
  • 1
    Related: https://stackoverflow.com/a/40312033/3776299 … but i'd go with git+ssh (and keys) instead – helb Dec 30 '18 at 16:22

1 Answers1

-2
  1. Create .env file at the same directory level where package.json resides.
  2. Mention PERSONAL_ACCESS_TOKEN=******************************* into .env file
  3. Don't forget to add .env into .gitingore list which will prevent exposing key to outside world while you make git commit to your repo.
  4. Now you can add your dependency in package.json as below,

"dependencies": {
...
  "my-private-github-repo": "git+https://${ENV.PERSONAL_ACCESS_TOKEN}@github.com/USER/abcd-repo-3.4.0.git",
  ...
}

There are other ways using 'DOTENV' npm package, but it could not do much when we are trying to resolve "Github" package dependency. Above seems to be straight forward solution.

Mohnish
  • 1,010
  • 1
  • 12
  • 20
Prashant Patil
  • 101
  • 1
  • 5
  • 1
    Doesn't work, interpolation of the token is not executed before npm install. Did you ever test this? Straight forward, but useless. –  Oct 28 '22 at 10:39
  • it will never work, the order of the code execute between dependencies and main project are different – Kevin Simple Dec 20 '22 at 20:20