Skip to content

Latest commit

 

History

History
108 lines (74 loc) · 4.62 KB

learning_javascript_own_packages.md

File metadata and controls

108 lines (74 loc) · 4.62 KB
path title
/learnings/javascript_own_modules
Learnings: Javascript: Publishing Own Modules

Table Of Contents

<<NPM_Publish_Steps>>

Aka what stages are taken by npm when you ask it to publish or install something.

prepublish -> prepare -> prepublishOnly -> prepack -> pack -> postpack -> publish -> postPublish -> USER INSTALLS -> prepublish -> prepare -> preinstall -> install -> postinstall

Source.

Doing stuff (only) before publishing it

NPM 3 introduces a new script field prepublishOnly. This script will only be run before actual publishing.

See also:

Doing (the same) stuff before publishing AND on package install

This is the behavior of the prepublish since npm 1.1.71. So prepublish isn't... just for prepublishing.

You should use prepublishOnly or preinstall instead. If you actually want to do the same thing two places use the new prepare script that does this.

Testing your script flow out

npm pack <-- does everything but the sending to the repo.

<<NPMPackageFieldsToSet>>

Making sure you don't publish a closed source package to a public registry

Two options: set "private": true OR specify the repo:

{
  publishConfig: {
      registry: "https://MY.PRIVATE.REGISTRY.EXAMPLE.COM"
  }
}

( can also do npm install --registry=.... or npm config set registry https://MY.PRIVATE.REGISTRY.EXAMPLE.COM )

<<NPM_Modules_And_Dependancies_Kinds>>

Three kinds of dependancies: regular dependancies, dev dependancies, and peer dependancies.

  • regular: Stuff your package uses but you don't expose that in interfaces to the outside world.
  • dev: Stuff you need for dev-ing your package (but not elsewhere)
  • peer: Stuff your packages uses and exposes this dependancy to the rest of the world

Peer dependancies often happen in libraries that implement common Express middleware, where you are modifying the Express application object. In this case, you want to make sure you're using the same version of Express as the application you're being included into, because maybe you're modifying their app object. In this case you don't want to be using a different version (ie the internal object may look different from your perspective, etc ec).

See also:

<<NPM_Modules_And_Transative_Dependancies>>

The following diagram is a pre NPM 3 version of how Node treated transative dependancies. It still does something like this, but things are flatter now (unless there's a conflict).

            my_app/
        +----+index.js
found! |          `const e = require("express")`
        |     node_modules/
        +-----> express/
                index.js
            +---+  `const u = require("underscore")`
    found!   |   node_modules/
            +---> underscore/
                    index.js
                +-----+ `const p = require("some_peer_dependency")`
  NOT HERE! UP! |          +
                +>         |
                           |
        +------------------+
found!  |
        +---->  some_peer_dependency/
                index.js

Because of this history / ability, transative dependancies are less of an issue here: a module can pull in rambda and it can be a totally different version than the rambda you have, as the file resolution order (in this pre npm 3 naive scenario) goes:

  1. Is there a peer node_modules folder to your current file / directory? EXIT: we found. Not? GOTO 2
  2. cd ../
  3. GOTO 1.

See also: