NPM
Notes are focused on the GitHub npm registry usage.
Basics
Some basic info.
Setup initial package .json file
npm init -y
Save packages under teh devDependencies object in package .json file. Not install when production used.
npm install --save-dev <package name>
Update an existing package to the latest
npm update <package name>
Uninstall an existing package
npm uninstall <package name>
Only production installation
npm install --production
Updates
Quick Tip: "npm outdated" and "npm update"
To check package updating status:
npm outdated
Update an existing package to the latest
npm update <package name>
Reference info on stackoverflow
npm i -g npm-check-updates
ncd -u
npm install
Sematic Versioning
Recommended to start with 1.0.0.
Publish
To
Publisha package- Add publishConfig for
https://npm.pkg.github.comin the package.json file."publishConfig": {
"registry": "https://npm.pkg.github.com/"
}, - Does not require
.npmrcwhen using publishConfig - Use
GITHUB_TOKENfor authenticating - Example (test functions)
- uses: actions/setup-node@v3
with:
node-version: '14'
registry-url: https://npm.pkg.github.com/
- name: Deploy to GH NPM
run: |
npm publish --only=production
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- Add publishConfig for
Note that you can have a private repo and a public package. Refer to access control. A user would still require to authenticate via a personal access token though, but would not need access to the org/repo containing the package.
Install
Requires an access token even if public; have to authenticate
To
Installa package- Use
.npmrcfile (Only pactical approach for local install)@s2technologies:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=${GH_PAT_FOR_TF} - The Personal Access Token (PAT)
reporead:packagesdelete_repo- if using tf to create projects
- Use
Can install without
.npmrcvia a Workflow and nopackage-lock.jsonusing setup-node- uses: actions/setup-node@v3
with:
node-version: '14'
registry-url: https://npm.pkg.github.com/
- name: Package Install
env:
NODE_AUTH_TOKEN: ${{ secrets.GH_PAT_FOR_TF}}
run: npm install- Note, when switching to a sub-folder can't get install to work without a
.npmrcfile
- Note, when switching to a sub-folder can't get install to work without a
Can log in via cli to authenticate a personal access token
$ npm login --registry=https://npm.pkg.github.com
> Username: USERNAME
> Password: TOKEN
> Email: PUBLIC-EMAIL-ADDRESS
From Repo
Reference - https://www.pluralsight.com/guides/install-npm-packages-from-gitgithub
npm install https://github.com/user_name/node_project_name
For specific branch add #branch-name
Example:
npm install https://github.com/s2technologies/testspace.test.functions#main
Notes
- You can install package via repo. The
package.jsonwill be updated. The repo can thus install usingpackage.json. But it will not work when using GitHub Workflow. The installation just hangs. Reference here. The workaround:- remove the dependency from
package.json - install in 2 steps in the workflow:
- run: npm install https://github.com/user_name/node_project_name
- run: npm install
- remove the dependency from
- Using a
privaterepo is more difficult (requiresssh,key)