NPM (node package manage)

Purpose: To implement the projectModule management[including: module installation and management], and the management of global modules;

  • The installation

    • After Node is installed, NPM πŸ‘‰nodejs.org/en/ is installed with a stable version (LTS long-term support version).

    • Check the installed version: node –version (node -v) NPM –version;

    • Locally installed modules are in the node_modules folder of the project directory;

    • View of modules installed globally: NPM root -g

    • Install in global features:

      • In general, modules installed globally can use commands, such as:
        • $NPM I pm2 -g πŸ‘‰ You can run the pm2 –version command
        • Why? πŸ‘‰ installed in the global module, there will be a xxx.cmd command file, so we can use XXX command;
      • All projects can use it, but there is a problem: version conflicts
      • So in real project development, we install modules locally rather than globally when we don’t have to
      • Only utility classes that require command operations are installed globally:
        • Pm2 helps start background services;
        • NRM switches download sources;
        • Yarn is similar to NPM, a faster module management tool.
        • Vue /cli Vue scaffolding
        • Create-react -app React scaffolding
    • Install to local project:

      • Features:
        • You cannot run a command directly. If you need to run a command, configure itScript commands can be executed;
        • It can be used in the current project, but not in other projects (if you want to use it, you need to install it separately), but there will not be multiple project version conflicts;
      • Operation steps:
        • $To enter the specified project directory
          • $NPM init -y Creates a package.json module management manifest (no need to create it again if it already exists); πŸ‘‰ Note: The name of the project folder should not appear special symbols, Chinese characters, capital letters, etc. (it is best to only appear lowercase letters, digits and underscores)

            • Role 1: Store the modules and version numbers that the project needs to depend on [development and production dependencies];
            • Function 2: Configure executable script commands.
            • Role 3: It can also provide some engineered packaged configuration items.
            • .
          • $NPM I XXX installs specified modules into production dependencies I XXX –save I XXx-s

            $NPM I XXX — save-dev installs the specified module into the development dependency I XXX -d production dependency: required for development and also required for deployment to the server [e.g. Axios]; Development dependencies: required only for development, not for deployment to the server [example: less];

          • Install the required modules as shown in package.json. Npmi installs both development and production dependencies; NPM I installs both development and production dependencies. Npmi installs both development and production dependencies; NPM I — Production only relies on installation;

    • Installation source: www.npmjs.com

      • Because it is a foreign website, so the speed is very slow;
      • To speed up the installation: @1 change the installation source;
        • Use CNPM as module management, which is based onTaobao Mirror sourceπŸ‘‰ $NPM I CNPM –global (exactly the same as NPM);
        • It can also be based onnrmTo switch the installation source πŸ‘‰ $NPM I NRM -g install globalnrm
          • NRM ls View the installation source.
          • NRM use XXX Uses a source;

        Do not use the @ 2npmAnd in doingyarnManagement as a module (recommended);

        • Yarn installation is much faster than NPM.
        • The syntax of YARN is different from that of NPM:
          • Install a dependency: yarn add XXX xxx@version xxx@latest xxx@next
          • Installation and production dependency: YARN add XXX;
          • Installation and development dependency: yarn add XXX -d;
          • Yarn install(or simply write yarn);
          • Remove a dependency: yarn remove XXX;

        πŸ‘‰ note:yarnIf you want to install global dependencies, use NPM instead.

  • use

    • πŸ‘‰ NPM install (I) XXX -g (–global) πŸ‘‰ Example: NPM I XXX -g

      • You can install multiple global devices at the same time, for example, NPM I AA BB CC-g
      • Note: if it is a [MAC] computer, it must be added before NPMsudoFor example, sudo NPM I AA bb cc-g
    • Based on NPM, you can install any version of the current module [Benefits] πŸ‘‰ NPM I XXX @3 πŸ‘‰ Install the latest version of the third generation [email protected] πŸ‘‰ Install the specified version number XXX @latest πŸ‘‰ Install the most stable version XXX @next port Install the early version

    • Install the required modules into your local project

      • Access the local directory πŸ‘‰ NPM I XXX
    • How to make commands available to modules installed locally (default modules installed locally cannot use commands);

      • Configure executable commands in package.json: Run the property name in “scripts”, for example, NPM run XXX
      • In the. Bin directory
  • uninstall

    • NPM uninstall XXX Uninstalls installed modules XXX -g Uninstalls globally installed modules

Pm2 (Node Service Startup management Tool)

  • If you start the service based on Node server.js, the window closes and the service stops immediately. However, we expect the service to continue normally as long as it is started and the window is closed [restart the computer, the service disappears and needs to be restarted]!
  • $pm2 start server.js –name CRM — $pm2 start server.
  • Pm2 list Displays the services that are being started.
  • Pm2 restart CRM If the background code is changed, you need to restart the service.
  • Pm2 stop CRM end the service (but still in the PM2 List);
  • Pm2 del CRM drop service from list;
  • $pm2 start…

If the pM2 fails to start properly, you can start the pM2 server based on Node server.js. If the pM2 server fails to start properly, you can start the PM2 server based on Node server.js.

Literacy 1:

  • The birth of a module:
    • Development stage:
    • Internal beta Phase (Alpha phase)
    • Open Beta (Beta)
    • Stable (stable) [There is a transition period, during which we want to install this version, we need to add @next]
    • NPM view XXX versions πŸ‘‰ View all versions of a dependency

Literacy 2: Open the terminal command window of the computer (DOS command window), execute the corresponding command;

  • [Windows] πŸ‘‰ win key +r πŸ‘‰ The operation window πŸ‘‰ is displayed. Enter CMD πŸ‘‰ to open the command window πŸ‘‰ and run the corresponding commandThis is the user root directory
  • To access the specified directory run the following command:
    • Enter CMD in the specified address bar.
    • Supported by some operating systemsShift + right-click πŸ‘‰ to open the command window here;
    • You can also access the specified disk directory based on the DOS command:
      • Drive letter: Goes to the specified disk.
      • CD File directory address: Go to the specified directory.
  • (MAC) πŸ‘‰ foundIn the end commandProgram open, this is equivalent to the user root directory down to execute the command, if you want to enter the specified directory can onlyCD + Path address(Drag the file directly in, you can have the path address)