Node.js

 
 
Node.js is a server-side software system designed for writing scalable Internet applications, notably web servers.
It is build on built on Chrome's JavaScript runtime. Programs are written on the server side in JavaScript, using event-driven, asynchronous I/O to minimize overhead and maximize scalability.

Node.js contains a built-in HTTP server library, making it possible to run a web server without the use of external software, such as Apache or Lighttpd, and allowing more control of how the web server works. Node.js enables web developers to create an entire web application in JavaScript, both server-side and client-side.

The latest Node.js version can be downloaded from: http://nodejs.org

Even numbered versions (0.4, 0.6, 0.8) are stable, and odd numbered versions (0.3, 0.5) are unstable. The stable releases are API-stable, which means that if you are using 0.8.1 and 0.8.2 comes out, you should be able to upgrade with no issues.

rss feed






Create package.json and publish package to npm registry for everyone to install it



Information
none

Operating system used
Windows Vista Home Premium SP 2

Software prerequisites
Node.js 0.10.11


Procedure
  1. Goto your node project folder where you want the package.json file to be created, for example:
    c:\myproject

    Note:
    All files and directories in this project will be later published to the npm registry.

  2. Create a package.json file. This will contain all the information for npm about your package.
    Type: npm init

    You should see and do the following:

    This utility will walk you through creating a package.json file.
    It only covers the most common items, and tries to guess sane defaults.

    See 'npm help json' for definitive documentation on these fields
    and exactly what they do.

    Use 'npm install <pkg> --save' afterwards to install a package and
    save it as a dependency in the package.json file.

    Press ^C at any time to quit.
    name: (nodejs)
    robertlie-github-example
    version: (0.0.0)
    0.0.1
    description:
    get a list of github user repositories
    entry point: (github.js)
    github2.js
    test command:
    [press Enter]
    git repository:
    [press Enter]
    keywords:
    list, github, user, repositories
    author:
    Robert Lie
    license: (BSD)
    [press Enter]
    About to write to c:\myproject\package.json:

    {
       "name": "robertlie-github-example",
       "version": "0.0.1",
       "description": "get a list of github user repositories",
       "main": "github2.js",
       "dependencies": {
          "express": "~3.2.6"
       },
       "devDependencies": {},
       "scripts": {
          "test": "echo \"Error: no test specified\" && exit 1"
       },
       "repository": "",
       "keywords": [
          "list",
          "github",
          "user",
          "repositories"
       ],
       "author": "Robert Lie",
       "license": "BSD"
    }

    Is this ok? (yes)
    npm WARN package.json [email protected] No repository field.
    npm WARN package.json [email protected] No readme data.


    Note:
    In this example the express module is a dependency, npm will fetch this module and install it.
    If you want to install the latest express version, enter: "express": "*"

  3. The package.json file is created.

    Note:
    If you never intend to publish a package to the npm registry, add "private": "true" to your package.json. This prevents accidental publication.

  4. Create an npm account at https://npmjs.org/, by typing:
    npm adduser

    You should see and do the following:

    Username: robertlie
    Password:
    enter_password
    Email:
    enter_email_address
    npm http PUT https://registry.npmjs.org/-/user/org.couchdb.user:robertlie
    npm http 201 https://registry.npmjs.org/-/user/org.couchdb.user:robertlie


    Note:
    You can use your account at https://npmjs.org/login.

  5. An npm account has been created. You can now publish your package to the npm registry.
    Type: npm publish

    Please be patient when publishing the package to the npm registry.
    It can take up to 2 minutes to finish the process.

    You should see the following:

    npm WARN package.json [email protected] No repository field.
    npm WARN package.json [email protected] No readme data.
    npm http PUT https://registry.npmjs.org/robertlie-github-example
    npm http 201 https://registry.npmjs.org/robertlie-github-example
    npm http GET https://registry.npmjs.org/robertlie-github-example
    npm http 200 https://registry.npmjs.org/robertlie-github-example
    npm http PUT https://registry.npmjs.org/robertlie-github-example/-/
    robertlie-github-example-0.0.1.tgz/-rev/1-2d45d4cc33f9130b8388a6a91c03e3f9
    npm http 201 https://registry.npmjs.org/robertlie-github-example/-/
    robertlie-github-example-0.0.1.tgz/-rev/1-2d45d4cc33f9130b8388a6a91c03e3f9
    npm http PUT https://registry.npmjs.org/robertlie-github-example/0.0.1/-tag/latest
    npm http 201 https://registry.npmjs.org/robertlie-github-example/0.0.1/-tag/latest
    + [email protected]




  6. The last line "+ [email protected]" indicates that the package "robertlie-github-example" is pushed to the npm registry. To verify this, open a browser and goto http://search.npmjs.org and enter the published package name in the search box.

    npm search for a package

  7. If you want to publish a new version of your package to the npm registry, do the following:
    • First update your code.
    • Change the version number in the package.json file using any text editor.
      In this example change version 0.0.1 to 0.0.2
    • Type: npm publish

  8. Any user can now use your package.
    Create a project directory, for example: c:\demo
    Goto this directory and type: npm install <package_name>

    For example:
    npm install robertlie-github-example

    You should see the following:

    npm http GET https://registry.npmjs.org/robertlie-github-example
    npm http 200 https://registry.npmjs.org/robertlie-github-example
    npm WARN package.json [email protected] No repository field.
    npm WARN package.json [email protected] No readme data.
    npm http GET https://registry.npmjs.org/express
    :
    :
    [email protected] node_modules\robertlie-github-example
    +-- [email protected] ([email protected], [email protected], [email protected],
    [email protected], [email protected], [email protected], [email protected],
    [email protected], [email protected], [email protected], [email protected])


  9. The package "robertlie-github-example" is located within directory "c:\demo\node_modules"

  10. To COMPLETELY remove the package from the npm registry, type:
    npm unpublish --force <package_name>

    For example:
    npm unpublish --force robertlie-github-example

    You should see the following:

    npm WARN using --force I sure hope you know what you are doing.
    npm http GET https://registry.npmjs.org/robertlie-github-example
    npm http 200 https://registry.npmjs.org/robertlie-github-example
    npm http DELETE https://registry.npmjs.org/robertlie-github-example/
    -rev/5-50071bf415651513ad16e9d0460db57a
    npm http 200 https://registry.npmjs.org/robertlie-github-example/
    -rev/5-50071bf415651513ad16e9d0460db57a
    - robertlie-github-example