UNPKG

@diyawanna/my-first-package

Version:
307 lines (198 loc) β€’ 6.07 kB
<!-- πŸ§ͺ 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 ```