jpg619/fix-accessibility-content-flow
@reggi/path-to-regexp
dependabot/npm_and_yarn/main/copy-to-clipboard-4.0.2
dependabot/npm_and_yarn/main/eslint-10.4.0
dependabot/npm_and_yarn/main/npmcli/eslint-config-7.0.0
dependabot/npm_and_yarn/main/proc-log-7.0.0
dependabot/npm_and_yarn/npm_and_yarn-826852524d
dependabot/npm_and_yarn/npm_and_yarn-ab9a7f4bc2
deprecate-totp-2fa
dhei/classic-tokens
gat-bypass-2fa-docs
jpg619/fix-accessibility-content-flow
jpg619/version-bump-tar-2
kartykp/gat-bypass-2fa-docs
kartykp/upgrade-path-to-regex
main
maitxn/version-bump-tar
patch-1
reggi/cache-based-on-version
reggi/dev-engines
reggi/fix-transform-prettier
reggi/overrides
update-search-sensitivity
| 1 | --- |
| 2 | title: Creating Node.js modules |
| 3 | redirect_from: |
| 4 | - /getting-started/creating-node-modules |
| 5 | --- |
| 6 | |
| 7 | Node.js modules are a type of [package][about-pkgs] that can be published to npm. |
| 8 | |
| 9 | ## Overview |
| 10 | |
| 11 | 1. [Create a `package.json` file](#create-a-package-json-file) |
| 12 | 2. [Create the file that will be loaded when your module is required by another application](#create-the-file-that-will-be-loaded-when-your-module-is-required-by-another-application) |
| 13 | 3. [Test your module](#test-your-module) |
| 14 | |
| 15 | ## Create a `package.json` file |
| 16 | |
| 17 | 1. To create a `package.json` file, on the command line, in the root directory of your Node.js module, run `npm init`: |
| 18 | - For [scoped modules][scoped-pkg], run `npm init --scope=@scope-name` |
| 19 | - For [unscoped modules][unscoped-pkg], run `npm init` |
| 20 | 2. Provide responses for the required fields (`name` and `version`), as well as the `main` field: |
| 21 | - `name`: The name of your module. |
| 22 | - `version`: The initial module version. We recommend following [semantic versioning guidelines][semver] and starting with `1.0.0`. |
| 23 | |
| 24 | For more information on `package.json` files, see "[Creating a package.json file][creating-pkg-json]". |
| 25 | |
| 26 | ## Create the file that will be loaded when your module is required by another application |
| 27 | |
| 28 | Create a file with the same name you provided in the `main` field. In that file, add a function as a property of the `exports` object. This will make the function available to other code: |
| 29 | |
| 30 | ``` |
| 31 | exports.printMsg = function() { |
| 32 | console.log("This is a message from the demo package"); |
| 33 | } |
| 34 | ``` |
| 35 | |
| 36 | ## Test your module |
| 37 | |
| 38 | 1. Publish your package to npm: |
| 39 | - For [private packages][priv-pkg-pub] and [unscoped packages][unscoped-pkg-pub], use `npm publish`. |
| 40 | - For [scoped public packages][scoped-pkg-pub], use `npm publish --access public` |
| 41 | |
| 42 | 2. On the command line, create a new test directory outside of your project directory. |
| 43 | |
| 44 | ``` |
| 45 | mkdir test-directory |
| 46 | ``` |
| 47 | |
| 48 | 3. Switch to the new directory: |
| 49 | |
| 50 | ``` |
| 51 | cd /path/to/test-directory |
| 52 | ``` |
| 53 | |
| 54 | 4. In the test directory, install your module: |
| 55 | |
| 56 | ``` |
| 57 | npm install <your-module-name> |
| 58 | ``` |
| 59 | |
| 60 | 5. In the test directory, create a `test.js` file which requires your module and calls your module as a method. |
| 61 | |
| 62 | 6. On the command line, run `node test.js`. The message sent to the console.log should appear. |
| 63 | |
| 64 | ## Resources |
| 65 | |
| 66 | <YouTube id="3I78ELjTzlQ" /> |
| 67 | |
| 68 | [about-pkgs]: about-packages-and-modules |
| 69 | [scoped-pkg]: about-scopes |
| 70 | [unscoped-pkg]: creating-and-publishing-unscoped-public-packages |
| 71 | [semver]: about-semantic-versioning |
| 72 | [creating-pkg-json]: creating-a-package-json-file |
| 73 | [priv-pkg-pub]: creating-and-publishing-private-packages#publishing-private-packages |
| 74 | [unscoped-pkg-pub]: creating-and-publishing-unscoped-public-packages#publishing-unscoped-public-packages |
| 75 | [scoped-pkg-pub]: creating-and-publishing-scoped-public-packages#publishing-scoped-public-packages |