How To Create And Publish Your First NPM Package

Web Dev Simplified
3 Jul 202107:38

Summary

TLDRIn this informative video, Kyle from Web Dev Simplified demystifies the process of creating an npm package. He guides viewers through setting up a local development environment, initializing a package with a detailed 'package.json', coding a simple module, and testing it. The tutorial also covers linking the package for local testing, addressing common pitfalls like login issues and email verification, and finally publishing the package to the npm registry. Additionally, Kyle explains how to handle naming conflicts by using scoped packages, providing a comprehensive guide for budding npm package creators.

Takeaways

  • πŸ“¦ Creating an npm package is not as daunting as it seems and can be quite straightforward.
  • πŸ“ It's important to have a 'package' folder for the npm code and a 'test' folder for local testing before publishing.
  • πŸ”— Initializing a git repository early helps to include GitHub information in the package.json.
  • πŸ“ The package.json file is crucial for describing the package, and it's recommended to fill it with as much information as possible.
  • πŸ› οΈ The script's main function, 'isWds', is an example of a simple module that returns true or false based on input.
  • πŸ”— Using 'npm link' allows for local testing of the package by creating a symbolic link to the package folder.
  • πŸ“œ The 'script.js' in the test folder demonstrates how to require and use the local npm package.
  • πŸ”’ Before publishing, ensure you are logged into npm with 'npm login' using your npm account credentials.
  • 🚫 Be aware that you may encounter a 403 error if your npm account email is not verified; verify it to proceed with publishing.
  • πŸ”„ After resolving any login or verification issues, use 'npm publish' to make the package publicly available.
  • πŸ” If the desired package name is taken, consider using a scoped package with a prefix like '@username/' to avoid naming conflicts.
  • πŸ“Œ When initializing a new package and intending to use a scope, use 'npm init --scope' to set up a scoped package.json file.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is creating and publishing an npm package.

  • What are the two folders mentioned in the video?

    -The two folders mentioned are the 'package' folder, where the npm package code is placed, and the 'test' folder, used for local testing of the package.

  • Why is it important to test an npm package locally before publishing?

    -Testing an npm package locally before publishing ensures that the package works as expected and helps to identify any potential issues before it is made available to others.

  • What command is used to initialize a git repository in the video?

    -The command used to initialize a git repository is not explicitly mentioned, but it involves copying code into a new repository and using the 'git init' command.

  • Why is a README file important for an npm package?

    -A README file is important because it provides information about the package, its purpose, and how to use it, which is helpful for users when they find the package on GitHub or npm.

  • What is the purpose of the 'package.json' file in an npm package?

    -The 'package.json' file describes the package, including its name, version, description, main entry point, and other metadata, which is essential for npm to understand and manage the package.

  • What is the name of the package being created in the video?

    -The package being created in the video is named 'is-wds'.

  • What does the 'is-wds' function do?

    -The 'is-wds' function takes a string as input and returns true if the string is equal to 'wds', and false otherwise.

  • How can you link an npm package for local testing?

    -You can link an npm package for local testing by navigating to the package directory and running the 'npm link' command.

  • What command is used to publish an npm package?

    -The command used to publish an npm package is 'npm publish'.

  • What error might you encounter when trying to publish an npm package and how to resolve it?

    -One might encounter a 403 error indicating a forbidden action, which can be resolved by verifying the email associated with the npm account. Additionally, for scoped packages, you need to specify '--access public' to publish them as public packages.

  • What is a namespaced or scoped package in npm?

    -A namespaced or scoped package in npm is a package that has a unique prefix, usually the username or organization name, followed by a slash, ensuring the package name is unique and avoiding conflicts with other package names.

  • How can you create a scoped package by default during initialization?

    -To create a scoped package by default during initialization, you can use the 'npm init --scope <scope-name>' command, which will automatically add the scope prefix to the package name in the 'package.json' file.

  • What is the significance of the 'main' field in 'package.json'?

    -The 'main' field in 'package.json' specifies the entry point of the package, which is the file that will be loaded when the package is required in a Node.js application.

  • What is the role of the 'keywords' field in 'package.json'?

    -The 'keywords' field in 'package.json' helps users find the package when searching on npm by providing relevant keywords associated with the package.

  • How can you ensure that your npm package is linked with your GitHub repository?

    -You can ensure that your npm package is linked with your GitHub repository by including the repository URL in the 'repository' field of the 'package.json' file.

Outlines

00:00

πŸ“¦ Creating and Testing an npm Package

This paragraph introduces the process of creating a simple npm package, emphasizing its ease despite initial intimidation. The video's host, Kyle from Web Dev Simplified, guides viewers through setting up a package folder and a test folder for local testing. He demonstrates initializing a Git repository, creating a basic README, and manually specifying package details in a package.json file. The main file, index.js, contains a function that checks if a string equals 'wds'. The paragraph concludes with instructions on linking the package for local testing using npm link and testing it in a script.js file within the test folder.

05:02

πŸ”— Publishing and Managing npm Packages

This paragraph delves into the publishing process of an npm package. After writing the package code, the host explains how to publish it using npm publish but encounters a 403 error due to an unverified email. After resolving this, another error prompts the need for setting the package access to public when using a scoped name. The video then covers the process of publishing a scoped package, including setting the correct access rights. It also addresses the issue of package name conflicts by introducing scoped or namespaced packages. The host provides a tip for initializing a package with a scope from the start. The paragraph ends with a call to action, inviting viewers to watch another video about favorite npm packages.

Mindmap

Keywords

πŸ’‘npm package

An npm package is a piece of reusable code that can be distributed and used in various Node.js projects. The video focuses on the process of creating, testing, and publishing an npm package. The example used is a simple package that checks if a string is 'wds', returning true or false accordingly. Understanding npm packages is central to the video's tutorial on contributing to the Node.js ecosystem.

πŸ’‘package.json

The `package.json` file is a crucial component in any npm package, as it contains metadata about the package, including its name, version, description, entry point, and dependencies. In the video, creating and customizing the `package.json` file is a key step, as it ensures the package is properly defined and can be published and used by others.

πŸ’‘npm init

`npm init` is a command used to create a `package.json` file interactively or with default settings using the `-y` flag. In the video, the creator opts to manually specify the values in the `package.json` to ensure accuracy and completeness, which is a recommended approach when developing an npm package.

πŸ’‘npm link

`npm link` is a command that allows developers to link their local npm package to other local projects for testing before publishing. The video demonstrates this command to show how a locally developed package can be tested in a separate folder, ensuring that the package works as expected before it is shared publicly.

πŸ’‘module.exports

`module.exports` is a Node.js feature that defines what a module exports for other files to use. In the video, the function `isWDS` is exported using `module.exports`, making it available for use in other scripts, which is a fundamental step in making any Node.js package reusable.

πŸ’‘git repository

A Git repository is a storage space where your project’s files and revision history are kept. The video shows how to initialize a Git repository for the npm package, which is important for version control and collaboration, and it also links the package with the GitHub repository in the `package.json`.

πŸ’‘scoped package

A scoped package in npm is one that is associated with a specific user or organization, denoted by an `@username/package-name` format. The video explains how to create a scoped package, which is particularly useful when a desired package name is already taken or when organizing packages under a specific namespace.

πŸ’‘npm publish

`npm publish` is the command used to publish an npm package to the npm registry, making it available for others to install and use. The video walks through this process, including handling errors related to account login and email verification, which are common issues when first publishing a package.

πŸ’‘test script

A test script is a piece of code written to verify that the npm package functions as intended. In the video, a `script.js` file is created to test the `isWDS` function locally using `npm link`, ensuring that the package behaves correctly before being published.

πŸ’‘versioning

Versioning in npm refers to the practice of assigning a version number to a package using semantic versioning (semver), which helps manage package updates and compatibility. The video sets the initial version to 1.0.0 and highlights the importance of maintaining correct versioning in `package.json` for clarity and dependency management.

Highlights

Creating an npm package is easier than it seems, offering a streamlined process for developers.

The tutorial begins with setting up two folders: 'package' for the npm code and 'test' for local testing.

Local testing of an npm package before publishing is emphasized for reliability.

Using 'npm init -y' provides default package.json values, but custom specification is recommended for detailed info.

Package.json serves as a description of the package's purpose and should contain comprehensive information.

The 'is-wds' package example returns true or false based on the input string 'wds'.

The main file 'index.js' contains the core functionality of the package.

Node module exports are demonstrated with a simple function in 'index.js'.

Before publishing, linking the package locally allows for testing its installation and functionality.

The 'npm link' command is used to create a symlink for local package testing.

A 'script.js' in the test folder demonstrates requiring and using the local npm package.

After local testing, the 'npm publish' command is used to make the package available on the npm registry.

Logging into npm is required before publishing a package.

Email verification post-account creation is necessary to avoid publish restrictions.

Publishing a package involves potential errors, such as name conflicts, which can be resolved with scoped packages.

Scoped packages use a namespace to avoid naming conflicts and are published with the 'access: public' flag.

The tutorial concludes with the successful publication of the 'is-wds' package and its installation via 'npm install is-wds'.

Alternative package names and scoped packages are discussed as strategies for naming and publishing.

Transcripts

play00:00

creating your first npm package may seem

play00:02

incredibly intimidating

play00:03

but it's actually surprisingly easy

play00:06

[Music]

play00:09

welcome back to web dev simplified my

play00:11

name is kyle and my job is to simplify

play00:13

the web for you so you can start

play00:14

building your dream project sooner so if

play00:16

that sounds interesting make sure you

play00:17

subscribe to the channel for more videos

play00:19

just like this

play00:20

now to get started on the left hand side

play00:22

of my screen you can see that i have two

play00:23

folders one called package

play00:25

and one called test folder this package

play00:27

folder is where we're going to be

play00:28

putting all the code for our npm package

play00:30

we're going to create

play00:30

and the test folder is just going to be

play00:32

for testing it out locally and that's an

play00:34

important thing to do anytime you create

play00:35

an npm package you should probably test

play00:37

it locally before you publish it

play00:39

the first thing i want to do is i want

play00:40

to cd into that package folder

play00:42

that way we're inside the package folder

play00:44

and on the right hand side here i've

play00:45

created a brand new git repository and

play00:47

i'm going to copy all of the code inside

play00:49

here by clicking this copy button

play00:51

pasting it down and it's just going to

play00:52

initialize a git repository for us

play00:54

and it's going to create a very basic

play00:56

readme so inside this package you can

play00:57

see we have a readme

play00:59

if i refresh the page over here you can

play01:00

see we have that readme the reason i'm

play01:02

doing that before initializing my

play01:04

package

play01:04

is because that way when we initialize

play01:06

our package json it's going to have all

play01:08

of our github information

play01:09

linked inside of it so now the next step

play01:11

is going to be to create that package

play01:13

json so normally you would type in npm

play01:15

init dash

play01:16

y and that will give you all the default

play01:17

values but in our case we actually want

play01:19

to specify all the values ourselves

play01:21

that way we can have the most

play01:23

information inside of our package json

play01:25

and the package json is essentially just

play01:26

a way to describe what your package does

play01:28

so it's really important to put as much

play01:30

information in here as possible

play01:31

so as you can see we're going to give

play01:32

this a package name in our case we're

play01:34

going to be creating a package called

play01:36

is wds and this package is just going to

play01:38

return true or false depending on if we

play01:40

pass it a string of wds

play01:42

the version will start out at 1.0 for a

play01:44

description

play01:45

is the string wds it doesn't really

play01:48

matter what we put for this our entry

play01:49

point will just say index.js that's fine

play01:51

we'll create that file in a little bit

play01:53

we don't have a test command so we're

play01:54

going to leave that blank our git

play01:55

repository as you can see has already

play01:57

been picked up automatically so we'll

play01:58

leave that in there

play01:59

and for keywords we'll just put wds in

play02:01

here as our only keyword

play02:02

author will be web dev

play02:06

simplified and for license we'll just

play02:08

leave that blank and

play02:09

yes so now if we go into our package

play02:10

json you can see all that information

play02:12

we've specified is inside of here which

play02:14

is exactly what we want

play02:15

now the important thing is that our main

play02:17

file here is index.js so we're going to

play02:19

create a file called

play02:20

index.js and inside here is where all of

play02:23

our code is going to go

play02:24

so the only thing we're going to have

play02:25

inside of here is a single function

play02:26

called is

play02:27

wds it's going to take in a string and

play02:31

we're just going to return the string

play02:33

equal to

play02:34

the text wds if it's equal to that it's

play02:36

going to return true otherwise false

play02:38

and then this is going to be a node

play02:39

module so we'll just say module.exports

play02:42

equals and we're going to export that is

play02:44

wds

play02:45

function super straightforward all it

play02:47

does is create one single function

play02:49

and now you may be thinking okay we

play02:50

wrote all the code for our package

play02:52

hopefully yours is a little bit more

play02:53

intense than this now you're ready to

play02:55

publish the package

play02:56

but actually what you first need to do

play02:57

is actually link the package so you can

play02:59

test it locally

play03:00

and the easiest way to do that is just

play03:02

type in the command npm

play03:04

link if you do this what's going to

play03:05

happen is npm is going to essentially

play03:07

say okay this thing in this package

play03:09

folder

play03:09

we're going to create a link for this

play03:11

thing that way when you try to install

play03:13

it somewhere else

play03:14

it's going to install from this location

play03:16

so that's where our test folder comes in

play03:17

we're just going to create a simple

play03:19

script.js file inside of here and in the

play03:22

script file we're going to require

play03:24

that is wds package

play03:28

and we're just going to say const is wds

play03:31

is equal to that

play03:32

and then we're going to return

play03:34

console.log

play03:35

is wds and we're going to put in the

play03:37

string wds so this should console log

play03:39

true but how do we actually install this

play03:41

package is wds because we haven't

play03:43

deployed it yet all we've done is link

play03:45

it

play03:45

so what we can do is navigate into that

play03:47

test folder

play03:49

and what we can just type in is npm link

play03:50

again and we just type in the name of

play03:52

our package is wds

play03:54

hit enter and that's going to actually

play03:55

locally install our package if we look

play03:57

in our node modules you can see we have

play03:58

is wds and inside there is the package

play04:01

that we already linked so as you can see

play04:02

it installed that package locally for us

play04:04

and now if we run this script js by just

play04:06

saying node script js

play04:08

you can see it returns true because is

play04:09

wds is true

play04:11

if we put in some random garbage in here

play04:13

and run this script you're going to see

play04:14

it returns false

play04:16

so now we have successfully tested our

play04:18

code we've written our code

play04:19

it's finally time to publish so let's go

play04:22

back into that package folder where our

play04:24

package exists and we can type in npm

play04:26

publish

play04:27

but you're going to notice when we do

play04:28

this we're going to get an error just

play04:29

give it a second you can see we have an

play04:30

error and essentially the problem is

play04:32

that we aren't actually logged in at all

play04:34

so what we need to do is log in

play04:35

you can type in npm login and you need

play04:38

to have an npm account to do this so

play04:39

make sure you go to npm and create an

play04:41

account but once you do you can type in

play04:42

npm login

play04:44

type in your username which in my case

play04:45

is web dev

play04:47

simplified and then you can type in your

play04:49

password as well

play04:51

and once you do that you can also type

play04:52

in your email and this is public so just

play04:54

make sure that you know that but i'm

play04:55

just going to type in my email here

play04:57

simplified.com and now you can see that

play05:00

we are logged in as web dev simplified

play05:02

so now if i try npm publish hopefully

play05:03

this time we don't get any errors

play05:05

so we just run that command and you'll

play05:06

notice we're still getting an error so

play05:08

let's see exactly what that error is you

play05:10

can see that it's saying we have a 403

play05:11

essentially saying that we're forbidden

play05:13

to

play05:13

make this package and the reason for

play05:15

this is that you need to make sure you

play05:16

verify your email after you create your

play05:18

account

play05:18

so i've gone ahead and i've verified my

play05:20

email so now if i run npm publish it

play05:22

should work this time so we're running

play05:24

npm publish

play05:25

as you can see it's doing all the

play05:26

loading so this actually looks like it's

play05:27

working and it's getting ready to

play05:28

publish my package

play05:30

and it looks like it just finished

play05:31

publishing the package now if we go on

play05:32

over to npm and we just search for

play05:34

is wds we should be able to see our

play05:36

package right here we can click

play05:38

on it and as you can see we can view our

play05:39

package as our homepage which links to

play05:40

our github same with the repository and

play05:42

so on so now we have our package

play05:44

officially published

play05:45

and we can install this package just

play05:46

like you would install a normal package

play05:48

with npm iwds

play05:50

now sometimes when you want to actually

play05:52

create a package the name is already

play05:54

going to be taken for example

play05:55

you couldn't create a package called is

play05:56

wds because i've already created that

play05:59

but the way you can get around this with

play06:00

npm is by using a name spaced or scoped

play06:03

package

play06:04

this is something that babel for example

play06:05

does as you can see everything is

play06:06

prefixed with at babel at babel so they

play06:08

can call their package

play06:10

at babel core and even though core is

play06:12

used other places

play06:13

it's okay because it's within that babel

play06:15

prefix if you want to do this all you

play06:17

need to do inside of your package json

play06:19

is just make sure you prefix the name of

play06:21

your package with your username so we

play06:23

could say at

play06:24

web dev

play06:27

simplified slash is wds and that's going

play06:30

to create a namespace package

play06:32

so now we go to publishes you're going

play06:33

to see that it's able to publish this

play06:35

package

play06:36

you'll notice we get an error it's

play06:37

saying that you must sign up for private

play06:39

packages

play06:40

the reason for this is because by

play06:41

default when you have a namespace

play06:43

package like this it tries to make it a

play06:45

private package instead of a public

play06:47

package that everyone can access

play06:48

so instead when you publish this you

play06:50

need to make sure you specifically say

play06:52

that the access is equal to

play06:55

public

play06:58

now as you can see that was successful

play07:00

and there were no errors so now we can

play07:01

do as we can say is wds

play07:03

and we have both the normal version as

play07:05

well as this name spaced version which

play07:07

we could call whatever we want

play07:09

and an important thing to note is if you

play07:10

want to make your package scoped by

play07:12

default when you initialize it just use

play07:14

npm

play07:15

init dash dash scope and set the scope

play07:17

equal to you know web

play07:18

dev simplified and that's going to

play07:21

create a scoped package json for you

play07:23

which is going to give the name this at

play07:25

in front of it

play07:26

and that's all it takes to create your

play07:27

very own npm package

play07:29

if you enjoyed this then you're

play07:30

definitely gonna love this video where i

play07:32

talk about my six favorite npm packages

play07:35

thank you very much for watching this

play07:36

video and have a good day

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
NPM PackageWeb DevelopmentTutorialPublish GuideCoding BasicsPackage.jsonLocal TestingGit RepositoryNode ModuleScoped Packages