Skip to content

Commit aa265d0

Browse files
committed
creating a new module
1 parent 65066ee commit aa265d0

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

readme.md

+59-2
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ NPM is different from most package managers in that it installs modules into a f
418418
419419
Many package managers install things globally. For instance, if you `apt-get install couchdb` on Debian Linux it will try to install the latest stable version of CouchDB. If you are trying to install CouchDB as a dependency of some other piece of software and that software needs and older version of CouchDB, you have to uninstall the newer version of CouchDB and then install the older version. You can't have two versions of CouchDB installed because Debian only knows how to install things into one place.
420420
421-
It's not just Debian that does this. Most programming language package managers work this way too. To address the global dependencies problem described above there have been virtual environment developed like [virtualenv](http://docs.python-guide.org/en/latest/dev/virtualenvs.html) for Python or [bundler](http://bundler.io/) for Ruby. These just split your environment up in to many virtual environments, one for each projects, but inside each environment dependencies are still globally installed. Virtual environments don't always solve the problem, sometimes they just multiply it by adding additional layers of complexity.
421+
It's not just Debian that does this. Most programming language package managers work this way too. To address the global dependencies problem described above there have been virtual environment developed like [virtualenv](http://docs.python-guide.org/en/latest/dev/virtualenvs.html) for Python or [bundler](http://bundler.io/) for Ruby. These just split your environment up in to many virtual environments, one for each project, but inside each environment dependencies are still globally installed. Virtual environments don't always solve the problem, sometimes they just multiply it by adding additional layers of complexity.
422422
423423
With NPM installing global modules is an anti-pattern. Just like how you shouldn't use global variables in your JavaScript programs you also shouldn't install global modules (unless you need a module with an executable binary to show up in your global `PATH`, but you don't always need to do this -- more on this later).
424424
@@ -450,7 +450,64 @@ To test out which module actually gets loaded by node, you can use the `require.
450450
451451
### How to write a module
452452
453-
** coming soon **
453+
Now that you know how to find modules and require them you can start writing your own modules.
454+
455+
#### The simplest possible module
456+
457+
Node modules are radically lightweight. Here is one of the simplest possible node modules:
458+
459+
`package.json`:
460+
```js
461+
{
462+
"id": "number-one",
463+
"version": "1.0.0"
464+
}
465+
```
466+
467+
`index.js`:
468+
```js
469+
module.exports = 1
470+
```
471+
472+
Put both of those files in a folder called `number-one` (the `id` in `package.json` must match the folder name) and you'll have a working node module.
473+
474+
Calling the function `require('number-one')` return the value of whatever `module.exports` was set to inside the module:
475+
476+
![simple-module](simple-module.png)
477+
478+
An even quicker way to create a module is to run these commands:
479+
480+
```sh
481+
mkdir my_module
482+
cd my_module
483+
git init
484+
git remote add git@github.com:yourusername/my_module.git
485+
npm init
486+
```
487+
488+
Running `npm init` will create a valid `package.json` for you and if you run it in an existing `git` repo it will set the `repositories` field inside `package.json` automatically as well!
489+
490+
#### Adding dependencies
491+
492+
A module can list any other modules from NPM or GitHub in the `dependencies` field of `package.json`. To install the `request` module as a new dependency and automatically add it to `package.json` run this from your module root directory:
493+
494+
```sh
495+
npm install --save request
496+
```
497+
498+
This installs a copy of `request` into the closest `node_modules` folder and makes our `package.json` look something like this:
499+
500+
```
501+
{
502+
"id": "number-one",
503+
"version": "1.0.0",
504+
"dependencies": {
505+
"request": "~2.22.0"
506+
}
507+
}
508+
```
509+
510+
By default `npm install` will grab the latest published version of a module.
454511
455512
## Going with the grain
456513

simple-module.png

95.1 KB
Loading

0 commit comments

Comments
 (0)