|
| 1 | +# How to Publish a Node.js Library to npm |
| 2 | + |
| 3 | +This document outlines the steps taken to publish the `piml.js` library to the npm registry. |
| 4 | + |
| 5 | +## 1. Initial Setup and Conversion |
| 6 | + |
| 7 | +- Creating a `piml.js` file to house the JavaScript library. |
| 8 | +- Creating a `piml.test.js` file to test the JavaScript library. |
| 9 | + |
| 10 | +## 2. Setting up the Node.js Project |
| 11 | + |
| 12 | +To prepare the project for npm, the following steps were taken: |
| 13 | + |
| 14 | +- **`package.json`:** A `package.json` file was created to manage the project's metadata and dependencies. It was populated with the following information: |
| 15 | + - `name`: The name of the package on npm (e.g., "piml"). |
| 16 | + - `version`: The initial version of the package (e.g., "1.0.0"). |
| 17 | + - `description`: A brief description of the package. |
| 18 | + - `main`: The entry point of the package (e.g., "piml.js"). |
| 19 | + - `scripts`: A "test" script to run the tests using Jest. |
| 20 | + - `keywords`: Keywords to help users find the package on npm. |
| 21 | + - `author`: The author of the package. |
| 22 | + - `license`: The license of the package (e.g., "MIT"). |
| 23 | + - `devDependencies`: The development dependencies, such as `jest`. |
| 24 | + |
| 25 | +- **`.gitignore`:** A `.gitignore` file was created to prevent unnecessary files from being committed to the repository, such as `node_modules`, logs, and system files. |
| 26 | + |
| 27 | +- **Dependencies Installation:** The development dependencies were installed by running `npm install`. |
| 28 | + |
| 29 | +## 3. Testing |
| 30 | + |
| 31 | +With the project set up, the tests were run to ensure the library was working correctly: |
| 32 | + |
| 33 | +```bash |
| 34 | +npm test |
| 35 | +``` |
| 36 | + |
| 37 | +Any failing tests were debugged and fixed until all tests passed. |
| 38 | + |
| 39 | +## 4. Publishing to npm |
| 40 | + |
| 41 | +Once the library was tested and ready, the following steps were taken to publish it to npm: |
| 42 | + |
| 43 | +1. **Create an npm Account:** An npm account is required to publish packages. You can create one at [https://www.npmjs.com/signup](https://www.npmjs.com/signup). |
| 44 | + |
| 45 | +2. **Log in to npm:** From the command line, you need to log in to your npm account: |
| 46 | + |
| 47 | + ```bash |
| 48 | + npm login |
| 49 | + ``` |
| 50 | + |
| 51 | + You will be prompted to enter your npm username, password, and email address. |
| 52 | + |
| 53 | +3. **Check Package Name Availability:** Before publishing, it's a good practice to check if the desired package name is available. This can be done by running: |
| 54 | + |
| 55 | + ```bash |
| 56 | + npm view <package-name> |
| 57 | + ``` |
| 58 | + |
| 59 | + If the package exists, you will see information about it. If it doesn't, you will get a 404 error, which means the name is available. |
| 60 | + |
| 61 | +4. **Publish the Package:** To publish the package, run the following command from the project's root directory: |
| 62 | + |
| 63 | + ```bash |
| 64 | + npm publish |
| 65 | + ``` |
| 66 | + |
| 67 | + If the package name is scoped (e.g., `@username/package-name`), you need to use the `--access public` flag: |
| 68 | + |
| 69 | + ```bash |
| 70 | + npm publish --access public |
| 71 | + ``` |
| 72 | + |
| 73 | +5. **Verify the Package:** After publishing, you can verify that the package is available on npm by visiting `https://www.npmjs.com/package/<your-package-name>`. |
| 74 | + |
| 75 | +By following these steps, the `piml.js` library was successfully published to the npm registry. |
0 commit comments