Movatterモバイル変換


[0]ホーム

URL:


Skip to searchSkip to content

Site navigation

Node.js modules are a type ofpackage that can be published to npm.

Overview

  1. Create apackage.json file
  2. Create the file that will be loaded when your module is required by another application
  3. Test your module

Create apackage.json file

  1. To create apackage.json file, on the command line, in the root directory of your Node.js module, runnpm init:
  2. Provide responses for the required fields (name andversion), as well as themain field:

For more information onpackage.json files, see "Creating a package.json file".

Create the file that will be loaded when your module is required by another application

Create a file with the same name you provided in themain field. In that file, add a function as a property of theexports object. This will make the function available to other code:

exports.printMsg=function(){
console.log("This is a message from the demo package");
}

Test your module

  1. Publish your package to npm:

  2. On the command line, create a new test directory outside of your project directory.

    mkdir test-directory
  3. Switch to the new directory:

    cd /path/to/test-directory
  4. In the test directory, install your module:

    npm install <your-module-name>
  5. In the test directory, create atest.js file which requires your module and calls your module as a method.

  6. On the command line, runnode test.js. The message sent to the console.log should appear.

Resources

Edit this page on GitHub
3 contributorsandrewnessinjimlukekarrysethomson
Last edited byandrewnessinjim onJune 24, 2024

[8]ページ先頭

©2009-2025 Movatter.jp