This template is designed to kickstart your journey in creating your own npm package. It provides a basic folder structure and initial files to get you started.
- Getting Started
- Project Structure
- Package.json Guide
- Gitignore File Guide
- Writing Your Code
- Publishing Your Package
To set up your project, follow these steps:
- Install the necessary dependencies with
npm install
. - Modify the
package.json
file to suit your project's needs. - Uncomment the necessary lines in the
.gitignore
file. - Build your project with
npm run build
. - Clear the existing sample code in
index.ts
,functions.ts
, andtypes.ts
. - Start writing your own functions or code.
- When ready, build and publish your code to npm.
The project is structured as follows:
.
├── src # Source files (push to GitHub)
│ ├── index.ts # Entry point of your package
│ ├── functions.ts # File where you define your package's functions
│ └── types.ts # File where you define your package's types
├── dist # Compiled code (push to npm)
├── node_modules # Directory for installed packages
├── .gitignore # File specifying which files and directories to ignore in Git
├── package.json # File containing metadata about your package
├── README.md # File containing documentation for your package
├── tsconfig.json # Configuration file for TypeScript
├── .npmignore
└── tsup.config.ts
The package.json
file contains important information about your project. Here's a guide to understanding and modifying it:
name
: Your package name.version
: The current version of your package.description
: A description of your package.main
: The entry point for your package. Default is./dist/index.js
.module
: The module entry point for your package. Default is./dist/index.mjs
.types
: The TypeScript definition file for your package. Default is./dist/index.d.ts
.homepage
: The homepage URL for your package.repository
: The URL of your package's repository.scripts
: Scripts that can be run from the command line. Default istsup
for the build script.keywords
: Keywords related to your package.author
: The author's name.license
: The license for your package. Default isMIT
.devDependencies
: Development dependencies for your package. Default aretsup
andtypescript
.
The .gitignore
file specifies which files and directories should be ignored by Git. Uncomment the following lines as needed:
/node_modules
/dist
.env
.DS_Store
The /src
folder is your starting point. It includes the index.ts
, functions.ts
, and types.ts
files. To start writing your own code:
- Open the file in your text editor.
- Delete the existing sample code.
- Start writing your own functions or code.
When you're ready to publish your package to npm:
-
Ensure your
/dist
directory has the latest code. Runnpm run build
:npm run build
-
Publish your package with
npm publish
:npm publish
-
Authenticate with your npm account.
Congratulations! Your npm package is now published. Great job!