【转载】NodeJS 学习笔记

References:

https://segmentfault.com/a/1190000017193008
https://www.nodebeginner.org/#serving-something-useful
https://www.npmjs.com.cn/

Contents:

Create a Node.js Module and Use it locally, refer to  https://segmentfault.com/a/1190000017193008

How to create a Node.js Module

  1. Create a new directory and enter it.   E.g. /d/sayHelloWorld
  2. Init your Node.js module.  Run command "npm init" to set your package.json file.    NOTE: npm init -y
  3. Create your entry point            e.g.  /d/sayHelloWorld/index.js
module.exports = {
    say() {
        console.log('Hello World!');
    }
}

How to user your Node.js Module locally

  1. Create a new directory and enter it.
  2. Install the Node.js module that you created before
  3. Enter into the Node shell interface, and test if your Node.js Module is available to use
node
> var test = require([Node.js Module Name])   // e.g. var test = require('sayhelloworld')
> test.say()

The Node Beginner Book, refer to https://www.nodebeginner.org/#serving-something-useful

Following was referred to https://www.npmjs.com.cn/

Install package locally

  1. Go to the directory that you want to place the package
  2. Run npm install [package name]

Update packages to local

  1. Go to the directory which package.json file was.
  2. Run npm update
  3. Run npm outdated, should have no output.

Uninstall package in local

  1. Go to node_modules directory.
  2. Run npm uninstall [package name]
  3. Run npm uninstall --save [package name]
  4. Run npm uninstall --save-dev [package name]

Install package globally

  1. Run npm install -g [package name]

Update package globally

  1. Run npm update -g [package name]
  2. Update all packages by running npm update -g               
  3. Find out which packages need to be updated by running npm outdated -g --depth=0

Uninstall packages globally

  1. Run npm uninstall -g [package name]

如何创建Node.js模板     --> see part 1 above.

How to work with scoped packages.

  1. If a package's name begins with @, then it is a scoped package. The scope is everything in between the @ and the slash.      @scope/project-name
  2. Each npm user has their own scope:   @username/project-name
  3. Initialize a scoped package
  4. Publishing a scoped package
  5. Using a scoped package
    1. In package.json   { "dependencies": { "@username/project-name": "^1.0.0" } }
    2. On command line:      npm install @username/project-name --save
    3. In require statement:   var projectName = require("@username/project-name")

Understanding packages and modules

  1. A package is a file or directory that is decribed by a package.json. This can happen in a bunch of different ways
  2. A module is any file or directory that can be loaded by Node.js' require(). Again, there are several configurations that allow this to happen.
  3. What is a package?
    A package is any of the following:
  • A folder containing a program described by a package.json file.
  • A gzipped tarball containing (a).
  • A url that resolves to (b).
  • A @ that is published on the registry with (c).
  • A @ that points to (d).
  • A that has a latest tag satisfying (e).
  • A git url that, when cloned, results in (a).

Noting all these package possibilities, it follows that even if you never publish your package to the public registry, you can still get a lot of benefits of using npm:

  • If you just want to write a node program, and/or,
  • If you also want to be able to easily install it elsewhere after packing it up into a tarball.

What is a module?

A module is anything that can be loaded with require() in a Node.js program. The following are all examples of things that can be loaded as modules:

  • A folder with a package.json file containing a main field.
  • A folder with an index.js file in it.
  • A JavaScript file.

Most npm packages are modules.

  • Generally, npm packages that are used in Node.js program are loaded with require, making them modules. However, there's no requirement that an npm package be a module.
  • Some packages, e.g., cli packages, only contain an executeable command-line interface and don't provide a main field for use in Node.js programs. These packages are not modules.
  • Almost all npm packages (at least, those that are Node programs) contain many modules within (because every file they load with require() is a module).
  • In the context of a Node program, the module is also the thing that was loaded from a file. For example, in the following program:   var req = require('request')
  • We might say that "The variable req refers to the request module".

File and Directory Names in the Node.js and npm Ecosystem

  • So, why is it the node_modules folder, but package.json file? Why not node_packages or module.json???
  • The package.json file defines the package.
  • The node_modules folder is the place Node.js looks for modules.
  • For example, if you create a file at node_modules/foo.js and then had a program that didi var f = require('foo.js'), it would load the module. However, foo.js is not a "package" in this case because it does not have a package.json.
  • Alternatively, if you create a package which does not have an index.js or a "main" field in the package.json file, then it is not a module. Even if it's installed in node_modules, it can't be an argument to require().

你可能感兴趣的:(前端之路,NPM)