# CommonJS Modules. Importación y Exportación

# El comando npm

# Ejercicio: Darse de alta en NPM

Para comenzar, crearemos una cuenta de usuario en el repositorio de NPM mediante la cual publicar nuestros propios paquetes:

Abrir el navegador.

Formulario para crear la cuenta en npmjs.org

  • Aceptar los términos de licencia.

  • Hacer clic en el botón Create an Account para crear la cuenta.

¡No olvide verificar su email!

Vaya a su cuenta de correo: debería tener un email de npm.

Este es un paso en el que muchos nos despistamos.

Una vez creada la cuenta, hay que abrir sesión con el servidor NPM para poder publicar paquetes en él. Abrir una consola.

Conectar al repositorio:

$ npm login

Rellene los datos que le solicita.

Consultar la cuenta con la que tenemos abierta la sesión:

$ npm whoami
1

Otra forma de darse de alta desde línea de comandos:

  • Ejecuta en la terminal estos comandos:

    		npm set init.author.name "Casiano Rodriguez-Leon"
    		npm set init.author.email "whatever@gmail.com"
    		npm set init.author.url "https://github.com/crguezl"
    
    1
    2
    3
  • El siguiente comando nos perdirá un email y una password, creando o verificando un usuario en el npm registry (opens new window), y guardará el token en el fichero ~/.npmrc:

    		npm adduser
    
    1

# Creación de Paquetes y Módulos en NodeJS

# Instalación desde GitHub

You can install packages directly from Github, and even specify a tag, sha, or branch if you want.

npm install https://github.com/ULL-ESIT-DSI-1617/scapegoat.git
npm install https://github.com/ULL-ESIT-DSI-1617/scapegoat.git#branch
1
2

must be https or git+ssh.

See How to install an npm package from GitHub directly? (opens new window) in StackOverflow

# Publicación con ámbito en una organización:

[/tmp/scapegoat(master)]$ npm publish --access public
+ @ull-esit-dsi-1617/scapegoat@1.0.2
1
2

# Scoped Packages

There are only two hard things in Computer Science: cache invalidation and naming things.

—Phil Karlton

Naming things is hard. It’s even harder when there are tens of thousands of other people who want to use the same names that you do. With hundred of thousands of modules on npm, it has been getting hard to find a name that isn’t taken.

Naming things just got a little bit easier for npm users with the introduction of scopes.

What are scopes?: Scopes are like namespaces for npm packages. Each npm user has their own scope.

@username/project-name
1

This means that you don’t have to worry about someone else taking your package name. Only you can add packages in your scope.

Scoped modules also make it possible to put your private code on npm when you sign up for private modules (opens new window). With private modules, you have control over who can see and collaborate on any of the modules in your scope.

Public scoped packages are free. To create a scoped package, all you need to do is add your scope to the front of the name property in package.json and run npm with the access option:

npm publish --access=public
1

# Package.json

# Ejemplo de package.json

	[~/javascript/evalua-module/scapegoat(master)]$ pwd -P
	/Users/casiano/local/src/javascript/evalua-module/scapegoat
	[~/javascript/evalua-module/scapegoat(master)]$ tree -I 'node_modules|docs'
	.
	├── LICENSE-MIT
	├── README.md
	├── index.js
	├── package.json
	└── test
			└── index.js

	1 directory, 5 files
	[~/javascript/evalua-module/scapegoat(master)]$ cat package.json
1
2
3
4
5
6
7
8
9
10
11
12
13
	{
		"name": "@ull-esit-dsi-1617/scapegoat",
		"version": "1.0.4",
		"description": "A small library providing utility methods to escape and unescape HTML entities",
		"main": "index.js",
		"scripts": {
			"test": "./node_modules/.bin/mocha --reporter spec",
			"doc": "documentation build index.js -f html -o docs"
		},
		"repository": {
			"type": "git",
			"url": "git@github.com:ULL-ESIT-DSI-1617/scapegoat.git"
		},
		"keywords": [
			"escape",
			"unescape",
			"html"
		],
		"author": "Casiano Rodriguez <casiano.rodriguez.leon@gmail.com>",
		"licenses": [
			{
				"type": "MIT",
				"url": "https://github.com/ULL-ESIT-DSI-1617/scapegoat/blob/master/LICENSE-MIT"
			}
		],
		"bugs": {
			"url": "https://github.com/ULL-ESIT-DSI-1617/scapegoat/issues"
		},
		"devDependencies": {
			"mocha": "*",
			"chai": "*"
		}
	}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

# Semantic versioning and npm

# npm Organizations /npm Organizaciones

Members of an Organization are immediately added to a Developers team that automatically has Read/Write access to all packages published under an Organization scope

# NPM: Herramientas de ayuda: release-it

Release a new patch (increments from e.g. 1.0.4 to 1.0.5):

release-it
1

Release a patch, minor, major, or specific version:

release-it minor
release-it 0.8.3
1
2

You can also do a "dry run", which won't write/touch anything, but does output the commands it would execute, and show the interactivity:

release-it --dry-run
1

# References: Tutorials

# NPM: Video Tutoriales

  1. What is npm? (opens new window)
  2. Installing Node.js and updating npm (opens new window)
  3. Fixing npm permissions (opens new window)
  4. Installing npm packages locally (opens new window)
  5. Using a package.json (opens new window)
  6. Updating local packages (opens new window)
  7. Uninstalling local packages (opens new window)
  8. Installing npm packages globally (opens new window)
  9. Updating global packages (opens new window) Uninstalling global packages (opens new window)11. Creating Node.js modules (opens new window)
  10. Publishing npm packages (opens new window)
  11. Semantic versioning and npm (opens new window)
  12. Working with scoped packages (opens new window)
  13. Using tags (opens new window)

# Yarn

# Creación de Paquetes en el Navegador/Cliente

# Documentation

Last Updated: a year ago