首页 > 解决方案 > 为什么 'npm start' 不能在带有 Node server.js 端口 :::3131 的浏览器中运行项目?(JavaScript 项目)

问题描述

大约一个月前,我有一个用 JS 编写的项目。从那以后我没有碰它。现在我想再次运行这个项目。

但是当我输入 Gitbash ' npm start ' 时,它并没有在浏览器中打开项目。

它停留在:' node server.js '并且没有更进一步。

昨天我在部署项目的端口 3131 上遇到了问题。我得到了信息,港口很忙。所以我在 CMD (windows) 中使用“netstat”命令查找所有打开的端口,然后使用“taskkill”终止监听端口 3131。

但即使在终止监听该端口后,“npm start”的效果与今天相同。

现在我以前的所有项目都在发生这种情况。所有这些都是在我在线编码学校的在线编码课程中写的。我大约一个月前完成了它。 它会以某种方式影响端口 3131 的问题吗?也许我应该改变端口?

重要信息:该项目已部署到 Heroku,但无法在那里工作。但即使在部署到 Heroku 后,“npm start”也能够在浏览器中运行项目(一个月前)。

顺便说一句,'npm run' 看起来工作正常,它运行所有测试等。


这是我的 package.json:

{
  "name": "fer-07-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.html",
  "scripts": {
    "lint": "eslint --fix --ext .js,.jsx .",
    "init-project": "npm install && npm-run-all init:*",
    "init:dirs": "mkdirp dist src/sass src/css src/vendor src/images src/js",
    "init:files": "touch README.md src/index.html src/sass/style.scss src/js/script.js",
    "init:gitignore": "curl https://raw.githubusercontent.com/github/gitignore/master/Node.gitignore -o .gitignore",
    "init:gitignore:custom": "echo \"dist\" >> .gitignore",
    "test": "npm-run-all test:*",
    "test:html": "globstar nu-html-checker dist/*.html",
    "test:js": "eslint src/js/ --fix",
    "test:scss": "stylelint src/sass/",
    "build": "npm-run-all build:* test",
    "build:clean": "mkdirp dist && rm -r dist && mkdirp dist",
    "build:copy": "copyfiles -a -u 1 -e \"**/sass/**/*\" -e \"**/.gitkeep\" \"src/**/*\" dist",
    "build:sass": "node-sass --output-style compact -o dist/css src/sass",
    "build:autoprefixer": "globstar autoprefixer-cli \"dist/css/*.css\"",
    "build-dev": "npm-run-all build-dev:sass build:autoprefixer",
    "build-dev:sass": "node-sass --output-style expanded --source-map true -o dist/css src/sass",
    "server": "json-server --port 3131 --no-cors --delay 250 --watch dist/db/app.json",
    "watch": "npm-run-all build build-dev -p watch:* server",
    "watch:browsersync": "browser-sync start --server dist --files \"dist/**/*\" --ignore \"dist/db/**/*\"",
    "watch:sassprefixer": "onchange \"src/sass/**/*.scss\" -- npm run build-dev",
    "watch:copy": "onchange -e \"**/sass/**/*\" -e \"**/.gitkeep\" \"src/**/*\" -- copyfiles -a -u 1 {{changed}} dist"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "flatpickr": "^4.6.9",
    "json-server": "^0.16.3",
    "rangeslider-pure": "^0.4.11"
  },
  "devDependencies": {
    "autoprefixer-cli": "^1.0.0",
    "browser-sync": "^2.26.3",
    "copyfiles": "^2.1.0",
    "eslint": "^5.14.1",
    "globstar": "^1.0.0",
    "mkdirp": "^0.5.1",
    "node-sass": "^4.11.0",
    "npm-run-all": "^4.1.5",
    "nu-html-checker": "^0.1.0",
    "onchange": "^5.2.0",
    "stylelint": "^9.10.1",
    "stylelint-scss": "^3.5.4"
  }
}

和 server.js:

/* global require, process */

const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('dist/db/app.json');
const middlewares = jsonServer.defaults({
  static: 'dist',
  noCors: true
});
const port = process.env.PORT || 3131;

server.use(middlewares);
server.use(router);

server.listen(port);

当使用“YARN START”启动另一个项目时,这是 GITBASH 中的一个问题

$ yarn start
yarn run v1.22.10
$ npm-run-all -p server:*
$ node server.js
$ webpack-dev-server --mode development --open --hot
events.js:292
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3131
    at Server.setupListenHandle [as _listen2] (net.js:1318:16)
    at listenInCluster (net.js:1366:12)
    at Server.listen (net.js:1452:7)
    at Function.listen (C:\Users\kpych.LENOVO-KP\OneDrive\Pulpit\Nowy folder (4)\Project-12_Travel_agency\node_modules\express\lib\application.js:618:24)
    at Object.<anonymous> (C:\Users\kpych.LENOVO-KP\OneDrive\Pulpit\Nowy folder (4)\Project-12_Travel_agency\server.js:32:10)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Emitted 'error' event on Server instance at:
    at emitErrorNT (net.js:1345:8)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'EADDRINUSE',
  errno: -4091,
  syscall: 'listen',
  address: '::',
  port: 3131
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
ERROR: "server:api" exited with 1.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Gitbash npm 启动

标签: node.jsnpmportnpm-startjson-server

解决方案


推荐阅读