@diyawanna/my-first-package
Version:
My first npm test package
307 lines (198 loc) β’ 6.07 kB
Markdown
<!-- π§ͺ Step 1: Create Your Package Project
mkdir my-first-package
cd my-first-package
npm init --scope=@diyawanna
Fill out the prompts (or use -y for default).
This will create a package.json with a scoped name like "name": "@diyawanna/my-first-package"
{
"name": "@diyawanna/my-first-package",
"version": "1.0.0",
"description": "My first npm test package",
"main": "index.js",
"scripts": {
"test": "echo \"No tests yet\""
},
"repository": {
"type": "git",
"url": "https://github.com/Diyawanna/my-first-package.git"
},
"keywords": [
"Diyawanna",
"Test",
"first"
],
"author": "@wsmr",
"license": "MIT",
"bugs": {
"url": "https://github.com/Diyawanna/my-first-package/issues"
},
"homepage": "https://github.com/Diyawanna/my-first-package#readme"
}
βοΈ Step 2: Write Your Package Code
Create a file called index.js:
// index.js
function sayHello(name) {
return `Hello, ${name}! From @diyawanna package.`;
}
module.exports = { sayHello };
**** SEE -> https://github.com/Diyawanna/my-first-package/commits/main/
π¦ Step 3: Update package.json (Optional Enhancements)
Make sure your package.json looks something like this:
{
"name": "@diyawanna/my-first-package",
"version": "1.0.0",
"description": "My first npm test package",
"main": "index.js",
"keywords": ["hello", "diyawanna", "test"],
"author": "Your Name",
"license": "MIT"
}
π Step 4: Test the Package Locally (Optional)
Before publishing, you can test it locally:
npm link
In another project:
npm link @diyawanna/my-first-package
Then use it like this:
const { sayHello } = require('@diyawanna/my-first-package');
console.log(sayHello("World"));
**** SEE -> https://github.com/Diyawanna/my-first-package/commits/main/
A.
cd /path/to/my-first-package
npm link
B.
cd /path/to/my-ionic-app
npm link @diyawanna/my-first-package
ionic serve
C.
import { sayHello } from '@diyawanna/my-first-package';
console.log(sayHello('Diyawanna'));
If it's TypeScript, make sure to build it first:
Add tsconfig.json if needed.
Build it:
tsc
Then link the compiled output (dist/index.js or similar).
D.
If You Update the Package
To see changes reflected in the app:
Rebuild your package (e.g. tsc if using TS).
It automatically updates in the linked project β no need to reinstall.
E.
When Youβre Done
To unlink and restore the original:
cd /path/to/my-ionic-app
npm unlink @diyawanna/my-first-package
npm install # to restore other packages if needed
π Step 5: Publish to NPM
If it's a public package:
npm publish --access public
π Without --access public, scoped packages are private by default, which requires a paid plan.
π§ͺ Step 6: Install & Use It Anywhere
After publishing, try installing it from any project:
npm i @diyawanna/my-first-package
Use it:
const { sayHello } = require('@diyawanna/my-first-package');
console.log(sayHello("Diyawanna"));
β
Summary
Step Description
npm init --scope=@diyawanna Initializes a scoped package
npm publish --access public Publishes it under your npm profile
npm i @diyawanna/your-package How others will install it
-->
# π¦ @diyawanna/my-first-package
> My first NPM test package β a simple utility that says hello π
> Published by [@diyawanna](https://www.npmjs.com/~diyawanna)
---
## β¨ Features
- πΉ Lightweight and easy to use
- πΉ Returns a custom greeting
- πΉ Useful as a base template for future NPM packages
---
## π Installation
Install the package via NPM:
```bash
npm install @diyawanna/my-first-package
```
## π§ Usage
### In JavaScript (CommonJS)
```js
const { sayHello } = require('@diyawanna/my-first-package');
console.log(sayHello("World"));
// Output: Hello, World! From @diyawanna package.
```
### In TypeScript / ES Modules
```ts
import { sayHello } from '@diyawanna/my-first-package';
console.log(sayHello('Diyawanna'));
// Output: Hello, Diyawanna! From @diyawanna package.
```
## π Local Development & Testing
### A. Link the Package Locally
In your package directory:
```bash
npm link
```
In your consuming project (e.g., Ionic or Laravel):
```bash
npm link @diyawanna/my-first-package
```
### B. Use in Your Project
**Example for Ionic Angular:**
```ts
import { sayHello } from '@diyawanna/my-first-package';
console.log(sayHello('Ionic App'));
```
**Example for Laravel (with a frontend build):**
```js
import { sayHello } from '@diyawanna/my-first-package';
console.log(sayHello('Laravel'));
```
### C. TypeScript Support (Optional)
If using TypeScript:
1. Add a `tsconfig.json` file.
2. Compile the source with:
```bash
tsc
```
## π οΈ Development Commands
- π§ **Build (if using TypeScript):** `tsc`
- π **Link to another project:** `npm link @diyawanna/my-first-package`
- β **Unlink when done:** `npm unlink @diyawanna/my-first-package`
## π¦ Publishing to NPM
To publish your package publicly (required for scoped packages like `@diyawanna/*`):
```bash
npm publish --access public
```
> βΉοΈ Without `--access public`, scoped packages are private by default.
## π Repository Structure
```
my-first-package/
βββ index.js # Entry point
βββ package.json # Package metadata
βββ README.md # This file
```
## π§ͺ Example Function
**index.js**
```js
function sayHello(name) {
return `Hello, ${name}! From @diyawanna package.`;
}
module.exports = { sayHello };
```
## π Issues & Feedback
Have a suggestion, question, or bug report?
π Open an issue on GitHub
## π License
MIT License
## π Author
Made with β€οΈ by @wsmr
Published under the @diyawanna namespace.
## π Useful Links
- π¦ **NPM Package:** [@diyawanna/my-first-package](https://www.npmjs.com/package/@diyawanna/my-first-package)
- π» **GitHub Repository:** [github.com/Diyawanna/my-first-package](https://github.com/Diyawanna/my-first-package)
---
### β
After Saving It
1. Place this in your project root as `README.md`.
2. Run:
```bash
npm publish --access public
```