hello-world-tutorial-_-a-tutorial-on-how-to-create-a-npm-package.
Version:
create your first npm package!
87 lines (59 loc) • 1.71 kB
Markdown
# How to Create Your First Hello World NPM Package
Creating your first npm package is easy and fun! Follow this step-by-step guide to get started.
## Step 1: Set Up Your Project Folder
1. Create a new folder for your project.
2. Navigate into the folder using your terminal.
```
mkdir my-first-npm-package
cd my-first-npm-package
```
## Step 2: Initialize Your Package
Run the following command to create a `package.json` file:
```
npm init
```
You'll be prompted to answer questions like package name, version, description, entry point, etc. You can press Enter to accept defaults or provide custom values.
## Step 3: Write Your Code
Create an `index.js` file inside your project folder with the following content:
```
function helloWorld() {
console.log("Hello, World!");
}
module.exports = helloWorld;
```
This simple function logs "Hello, World!" to the console.
## Step 4: Test Your Code Locally
Run the following command in your terminal to test your code:
```
node index.js
```
You should see "Hello, World!" printed in the console.
## Step 5: Publish Your Package
1. Log in to npm using:
```
npm login
```
2. Publish your package using:
```
npm publish
```
Your package is now live on npm!
## Step 6: Install and Use Your Package
To use your package in another project:
1. Install it via npm:
```
npm install <your-package-name>
```
2. Require and use it in your code:
```
const helloWorld = require('<your-package-name>');
helloWorld();
```
Congratulations! You've created and published your first npm package!