About using NPM

一个新手
Release: 2018-05-12 10:27:10
Original
3030 people have browsed it


First of all, what is NPM?

——Node Package Manager. It is a package management tool for js.

Without further ado, download it first. (npm is released together with Node.js. As long as Node.js is installed, npm will also be installed)
After the installation is complete, the first thing to do is of course to test whether the installation is successful. Press win + R to open the cmd command prompt and enter npm -v. A prompt appears that the version was successfully installed.

npm -v3.10.10
Copy after login

If you installed an old version of npm, you can easily upgrade it through the npm command.
npm install npm@latest -g

Use the npm command to install the module

First create a new project folder on your computer and find the folder on cmd
C:\Users\filbert\Desktop> cd app2
C:\Users\filbert\Desktop\app2>

Initialize npm, npm init –yes

C:\Users\filbert\Desktop\app2>npm init --yes Wrote to C:\Users\filbert\Desktop\app2\package.json: { "name": "app2", "version": "1.0.0", "description": "", "main": "app2.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC"}
Copy after login

Install the express framework, npm install express

C:\Users\filbert\Desktop\ch3>npm install express –save npm WARN ch3@1.0.0 No description npm WARN ch3@1.0.0 No repository field. + express@4.15.4 updated 1 package in 3.63s
Copy after login

To build a node server in the project, you must first create a new js file, such as app2.js

const path = require('path');const express = require('express'); const app = new express();const port = 4000;//app.use(express.static('public')); app.get('/*', (req, res) => { /*const pathname = req.params['0']; if(!pathname) { res.sendFile(path.join(__dirname, 'index.html')); return; }*/ res.sendFile(path.join(__dirname+'/index.html')); });var server = app.listen(port, (error) => { if (error) { console.error(error); } else { console.info('==> Listening on port %s. Open up http://localhost:%s/ in your browser.', port, port); } });
Copy after login

When you want to execute the server built by node.js, enter node app2.js## in cmd #

C:\Users\filbert\Desktop\app2>node app2.js ==> Listening on port 4000. Open up http://localhost:4000/ in your browser.
Copy after login
Copy http://localhost:4000/ to view the page


The above is the detailed content of About using NPM. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn