Web-Node Package Manager (NPM)

NodeJs installation

        I’ve written about it before, so I’ll just link to it.

        Node.js installation-CSDN blog

NPM

        In addition to Node, NPM or the Node Package Manager, there is an application installed with NodeJS, but it is developed separately and is updated on a different schedule than Node. It enables you to download packages from a central package registry, which are reusable JavaScript modules  https://www.npmjs.com

        NPM is the world's largest software registry, with over 800,000 packages. It is a free tool that includes a CLI for downloading and installing software packages.

        To install any package using npm, go to any CLI like Powershell or Terminal and enter the following:

npm install <package>

        Install npm:

        npm is installed with NodeJS, so you can access it immediately after downloading and installing NodeJS

        However, there is a JSON file called package.json in the root directory of the Javascript/Node project. This file contains metadata related to the project and is used to track the project’s dependencies, scripts, versions, etc. Npm uses this file to identify and manage the project's dependencies, as well as provide information for identifying and managing the project. A minimal package.json can be seen below:

{
  "name" : "mavic",
  "version" : "0.0.1"
}

        It may also include other metadata, such as a description of the project, its version as it appears in a specific release, license information, or configuration information.

        The root directory of most Node/NPM projects will contain a file called package.json. It contains metadata information that NPM (and Node, at least indirectly) needs to know about your project if it is to handle certain tasks. In this case it will instruct NPM which modules need to be installed if they are not already installed, making it very easy to give the project to another developer. This will also include information such as project name and version.

        Create a new directory and name it new-project. Then browse to the folder on the CLI (Powershell or Terminal). Enter the following

npm init

        This will initialize the package.json file in the folder and prompt for several details as shown in the video below. After entering all the details, you will see a package.json file in the same folder containing the filled-in information.

        After installing NodeJS on your computer, you can test whether npm is installed using the following command

npm -v

        NPM package modules enable Javascript developers to efficiently load dependencies. In order to load dependencies we need to run the following command.

npm install

        Using this command, you will search the root directory for a JSON file named  package.json  in order to install all dependencies defined in this file.

Update package

        You may occasionally want to update a project's dependencies after setting up the project. To do this you have to run the following command

npm update

Using NodeJS on CLI

        To use NodeJS on the command line interface (CLI), go to Powershell or Terminal and type

node

        You will see an interface similar to the one below.

        Any operation in a static application can be performed here, including importing libraries, creating functions, etc.

        So enter the following JavaScript code after >

var product = (number1, number2) => { return number1 * number2};

        Then call the function by sending some parameters as shown below

product(5, 3);

        The output can be seen in the screen below

NodeJS commands

        Here are some of the most commonly used commands when using NPM:

        Installation package

npm install <package_name>
        To install Install, we can use i
        During the installation process, there may be some warnings. Warning is nothing to worry about. After installing the local package, the project folder should contain  node_modules , package.json  , and  package-lock.json . Node_modules is where our local packages are installed. A new file named package-lock.json will be created   . Unlike package.json, which contains semantic versions, this file specifies the exact version of the package.
        To install a package globally, you need to use the -g flag  or --global
npm install -g <package_name>

        To update a package you need to use  update  or  up

npm update <package_name>

        Update global package

npm update <package_name> -g

        By default, any packages specified during npm installation are added to the dependencies. Using some extra flags we can modify whether and how they are saved:

        To save packages in devDependencies: -D  or  --save -dev

        Save package in optional dependencies: -O  or  --save -optional

        To prevent saving to dependencies: --no -save

        To uninstall a package you can use  uninstall  or un

npm uninstall <package_name>

        Uninstall global package

npm uninstall <package_name> -g

        To get a list of installed packages you can use  list  or  ls

npm ls

         Setting -all in npm ls  will show all installed packages, not just those directly required by the current project.

npm ls --all

        Get help from NPM CLI

npm help

        Get help for a specific command

npm <command> -h

        Search npm documentation help

npm help-search <command>

Set up a web server and start with NPM

        Create a new directory and initialize it as a project using NPM to set up the new web server. Adding -y will make all prompts use default values ​​instead of entering details.

npm init -y

        Then, you need to create a new JavaScript file called server.js in the same folder and enter the following code into the file.

require("http").createServer((req, res) => {
  res.end("Hello world");
}).listen(3030);

        Save the file and go to the terminal. Enter the following command.

npm start

        Then enter the browser and enter localhost:3030 to see helloworld

        If any errors occur, you must go to package.json and add the following to "Script:"

"scripts": {
    "start": "node your-script.js"
}

Guess you like

Origin blog.csdn.net/qq_54813250/article/details/133394853