# Publishing to npm — Node.js Fundamentals: Modules, Async and Servers

Source: https://www.skillbyai.com/en/nodejs/node-npm-publish

> Publish a package to the npm registry: unique name, versioning, npm login, bin field and npm publish.

## What publish needs

A unique `name`, a `version`, an `npm login`, and a `bin` field if it's a CLI tool — then `npm publish` uploads it to the registry.

## publish workflow

Bump the version before every publish — npm rejects re-publishing the same version number.

```bash
npm login
npm version patch      # 1.0.0 -> 1.0.1
npm publish
```

**Quiz:** Why does npm version patch matter before publishing an update?

- [x] npm refuses to publish a version number that already exists on the registry
- [ ] It compiles the code
- [ ] It deletes the previous version
- [ ] It uploads the code automatically

*Answer:* npm refuses to publish a version number that already exists on the registry. The registry treats each version as immutable — you must bump the number to publish new changes.

## Final quiz 1 of 8

Final quiz

**Quiz:** What makes Node able to handle many connections with one thread?

- [ ] One thread per request
- [ ] Compiling to C++
- [x] Non-blocking I/O and the event loop
- [ ] A built-in database

*Answer:* Non-blocking I/O and the event loop. Waiting I/O is handed off while the loop serves other work.

## Final quiz 2 of 8

Final quiz

**Quiz:** Which field enables ES module syntax in package.json?

- [ ] "main": "esm"
- [ ] "es": true
- [ ] "import": "all"
- [x] "type": "module"

*Answer:* "type": "module". Set type to module (or use .mjs).

## Final quiz 3 of 8

Final quiz

**Quiz:** What does ^4.19.2 allow?

- [x] Minor and patch updates below 5.0.0
- [ ] Only patch updates
- [ ] Major updates
- [ ] No updates

*Answer:* Minor and patch updates below 5.0.0. Caret allows compatible updates within the same major version.

## Final quiz 4 of 8

Final quiz

**Quiz:** Why use fs/promises inside a server handler?

- [ ] It is shorter
- [x] It does not block the event loop
- [ ] It encrypts files
- [ ] It needs no import

*Answer:* It does not block the event loop. Async file calls let other requests continue.

## Final quiz 5 of 8

Final quiz

**Quiz:** Which method connects a readable stream to a writable stream with backpressure?

- [ ] push
- [ ] emit
- [x] pipe (or pipeline)
- [ ] spawn

*Answer:* pipe (or pipeline). pipe and stream.pipeline manage flow control for you.

## Final quiz 6 of 8

Final quiz

**Quiz:** How do you run independent async calls together?

- [ ] Await them one by one
- [ ] setTimeout
- [ ] process.exit
- [x] Promise.all

*Answer:* Promise.all. Promise.all runs them concurrently and waits for all.

## Final quiz 7 of 8

Final quiz

**Quiz:** What should you do after an uncaughtException?

- [x] Log it and exit the process
- [ ] Ignore it
- [ ] Retry forever
- [ ] Delete node_modules

*Answer:* Log it and exit the process. The process may be in an unknown state, so exit and let a supervisor restart it.

## Final quiz 8 of 8

Final quiz

**Quiz:** Which Node module moves CPU-heavy work off the main thread?

- [ ] path
- [x] worker_threads
- [ ] os
- [ ] dotenv

*Answer:* worker_threads. Worker threads keep the event loop responsive.
