【问题标题】:Error occurs when using DEBUG=express:* npm start command in node.js在 node.js 中使用 DEBUG=express:* npm start 命令时发生错误
【发布时间】:2021-01-15 09:42:49
【问题描述】:

当我尝试在 Windows 上运行 DEBUG=express:* npm start 命令时,出现如下错误:

 'DEBUG' is not recognized as an internal or external command, operable program or batch file.

我已经在 stackoverflow 上搜索过类似主题的解决方案,但它们都不适合我。

我尝试了以下方法:

1. 我安装了调试库:npm install debug

2.设置DEBUG=express:* & node bin/www

报错:

                    Error: Cannot find module '../app'
                    Require stack:
                    - C:\Users\leo\notes\bin\www
                 

如果有人知道如何处理,请分享您的答案。

我的 www 文件代码是:

 #!/usr/bin/env node

  /**
   * Module dependencies.
  */

  var app = require('../app');
  var debug = require('debug')('notes:server');
  var http = require('http');

  /**
  * Get port from environment and store in Express.
  */

  var port = normalizePort(process.env.PORT || '3000');
  app.set('port', port);

  /**
   * Create HTTP server.
  */

  var server = http.createServer(app);

  /**
  * Listen on provided port, on all network interfaces.
  */

 server.listen(port);
 server.on('error', onError);
 server.on('listening', onListening);

 /**
  * Normalize a port into a number, string, or false.
 */

 function normalizePort(val) {
    var port = parseInt(val, 10);

    if (isNaN(port)) {
       // named pipe
       return val;
    }

    if (port >= 0) {
        // port number
        return port;
    }

   return false;
  }

 /**
  * Event listener for HTTP server "error" event.
 */

 function onError(error) {
    if (error.syscall !== 'listen') {
       throw error;
    }

    var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

    // handle specific listen errors with friendly messages
    switch (error.code) {
          case 'EACCES':
               console.error(bind + ' requires elevated privileges');
               process.exit(1);
          break;
          case 'EADDRINUSE':
               console.error(bind + ' is already in use');
               process.exit(1);
          break;
          default:
      throw error;
     }
   }

 /**
   * Event listener for HTTP server "listening" event.
 */

  function onListening() {
     var addr = server.address();
     var bind = typeof addr === 'string'
     ? 'pipe ' + addr
     : 'port ' + addr.port;
     debug('Listening on ' + bind);
  }

当我将 var app = require('../app'); 更改为 var app = require('../app.mjs');www 文件,然后我收到其他错误:

           C:\Users\leo\notes>set DEBUG=express:* & node bin/www
           internal/modules/cjs/loader.js:983
           throw new ERR_REQUIRE_ESM(filename);
           ^
                  

package.json文件内容为:

{
  "name": "notes",
  "version": "0.0.0",
  "private": true,
   "scripts": {
   "start": "cross-env DEBUG=notes:* node ./app.mjs",
   "server1": "cross-env DEBUG=notes:* PORT=3001 node ./app.mjs",
   "server2": "cross-env DEBUG=notes:* PORT=3002 node ./app.mjs"
    },
    "dependencies": {
         "cookie-parser": "^1.4.5",
         "cross-env": "^7.0.2",
         "debug": "~2.6.9",
         "express": "^4.17.1",
         "hbs": "^4.0.6",
         "http-errors": "~1.6.3",
         "morgan": "^1.9.1"
    }
  }

【问题讨论】:

  • 您如何要求您的app 文件?看来,当您的 bin/www 脚本运行时,它找错了地方。
  • @JatinMehrotra 不,我已经尝试过了
  • 相对于 bin/www 的 app.js 文件在哪里?
  • @Phix 我的 app.mjs 文件在 bin 文件夹之外

标签: node.js express


【解决方案1】:

似乎没有安装“调试”库。使用以下命令安装并重试。

npm install debug

【讨论】:

  • DEBUG 是一个环境变量。第一次没有工作,因为这是基于 nix 的系统的语法,Leo 在 Windows 上。
猜你喜欢
  • 1970-01-01
  • 2021-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-11
相关资源
最近更新 更多