-
Notifications
You must be signed in to change notification settings - Fork 53
Publishing an NPM package
Gingertonic edited this page Nov 3, 2020
·
2 revisions
We're starting to get cosy with installing npm packages that other people and companies have made using the npm install <package-name>
command. It would be great to start sharing our own handy little chunks of code with others!
- Check to see if you are logged in with
npm whoami
. If you are (your npm username is returned) continue to the next step. - If not,
npm adduser
- Update if prompted
- Check your email to login and verify your email address with the npm registry
This step is optional but recommended
mkdir <project-name>
cd <project-name>
git init
git remote add origin <your-repo-url>
npm init
- Check out the resulting
package.json
and update as you see fit
Ours looks like this:
{
"name": "hello-futureproof",
"version": "1.0.0",
"description": "futureproof npm package demo",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/getfutureproof/fp_study_notes_publishing_npm_packages.git"
},
"author": "futureproof",
"license": "ISC",
"bugs": {
"url": "https://github.com/getfutureproof/fp_study_notes_publishing_npm_packages/issues"
},
"homepage": "https://github.com/getfutureproof/fp_study_notes_publishing_npm_packages#readme"
}
- create a file called
index.js
(or something else, as long as you update"main"
in yourpackage.json
) - export a function from
index.js
usingmodule.exports = <myFunction>
- this is what will run when a user runs your package- you can write this as an anonymous function or point to a function defined elsewhere in your code
module.exports = function(name){
return `Hello from npm, ${name}! Welcome to futureproof!`
}
- if you want to export more than one function, export an object with keys that point to the functions:
function plus(num1, num2){
return num1 + num2
}
function minus(num1, num2){
return num1 - num2
}
module.exports = {
add: plus,
subtract: minus
}
npm version 1.0.0
Only if you made a git repo before!
- you know how to do this!
- additionally, run
git push --tags
npm publish
- That's it! Check out your handiwork at
https://www.npmjs.com/package/<your-package-name>
- You can test your package in a sandbox at
https://npm.runkit.com/<your-package-name>
. See example here.
Others can now install your package to their node project with npm install <your-package-name>
and use as any other package!
const fp = require('hello-futureproof');
let greeting = fp('Beth')
console.log(greeting)
If you package exported an object rather than a function, use accordingly:
const calc = require('my-calc-package');
let result = calc.add(4, 2)
console.log(result)
For more information including how to make a CLI executable package, check out the official documentation
For more information on our transformative coding education, visit us at https://www.lafosseacademy.com/