node.js npm start 在错误的目录中寻找/bin/www,如何更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26429209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:00:50  来源:igfitidea点击:

npm start looking for /bin/www in the wrong directory, how to update

node.jsnpm

提问by subpath42

So I'm following This Tutorial hereto get a personal/learning node.js project off the ground that I would like to work on and store in GitHub and eventually put onto a live server or cloud hosting. It's using node.js, express, monk, mongoDB for a website setup. However I have a persistent error that I haven't found an answer to when I run 'npm start'.

因此,我正在关注这里的本教程,以开始一个个人/学习 node.js 项目,我希望在 GitHub 上工作并存储该项目,并最终放到实时服务器或云托管上。它使用 node.js、express、mongo、mongoDB 进行网站设置。但是,当我运行“npm start”时,我有一个持续性错误,我没有找到答案。

Here is my command window output when I try to run 'npm start' inside my node express project.

这是我尝试在 node express 项目中运行“npm start”时的命令窗口输出。

I know that: Error: Cannot find module 'C:\Users\username\Desktop\bin\www' is not where my bin\www is located (it's inside the nodetest1 project).

我知道:错误:找不到模块 'C:\Users\username\Desktop\bin\www' 不是我的 bin\www 所在的位置(它在 nodetest1 项目中)。

C:\Users\username\Documents\GitHub\TEDResources\node.js\nodetest1 <-- correct path

C:\Users\username\Documents\GitHub\TEDResources\node.js\nodetest1 <-- 正确路径

So, why is npm looking at that incorrect spot and how do I update it to correctly look at my project folder? I've searched my system and can't find module.js anywhere. (using windows 7 x64) I've also tried nuking node.js and express and started from scratch without luck.

那么,为什么 npm 会查看那个不正确的位置,我该如何更新它以正确查看我的项目文件夹?我已经搜索了我的系统,但在任何地方都找不到 module.js。(使用 Windows 7 x64)我也试过 nuking node.js 和 express 并从头开始没有运气。

//console error output

//控制台错误输出

Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\username\Documents\GitHub\TEDResources\node.js\nodetest1>npm start

Microsoft Windows [版本 6.1.7601] 版权所有 (c) 2009 Microsoft Corporation。版权所有。C:\Users\username\Documents\GitHub\TEDResources\node.js\nodetest1>npm start

> [email protected] start C:\Users\username\Documents\GitHub\TEDResources\node.j
s\nodetest1

> node ./bin/www
module.js:340
    throw err;
          ^
Error: Cannot find module 'C:\Users\username\Desktop\bin\www'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

npm ERR! [email protected] start: `node ./bin/www`
npm ERR! Exit status 8
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is most likely a problem with the nodetest1 package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./bin/www
npm ERR! You can get their info via:
npm ERR!     npm owner ls nodetest1
npm ERR! There is likely additional logging output above.
npm ERR! System Windows_NT 6.1.7601
npm ERR! command "C:\Program Files\nodejs\\node.exe" "C:\Program Files\nod
ejs\node_modules\npm\bin\npm-cli.js" "start"
npm ERR! cwd C:\Users\username\Documents\GitHub\TEDResources\node.js\nodetest1

npm ERR! node -v v0.10.32
npm ERR! npm -v 1.4.28
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     C:\Users\username\Documents\GitHub\TEDResources\node.js\nodetest1
\npm-debug.log
npm ERR! not ok code 0

//npm-debug.log

//npm-debug.log

0 info it worked if it ends with ok
1 verbose cli [ 'C:\Program Files\nodejs\\node.exe',
1 verbose cli   'C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js',
1 verbose cli   'start' ]
2 info using [email protected]
3 info using [email protected]
4 verbose node symlink C:\Program Files\nodejs\node.exe
5 verbose run-script [ 'prestart', 'start', 'poststart' ]
6 info prestart [email protected]
7 info start [email protected]
8 verbose unsafe-perm in lifecycle true
9 info [email protected] Failed to exec start script
10 error [email protected] start: `node ./bin/www`
10 error Exit status 8
11 error Failed at the [email protected] start script.
11 error This is most likely a problem with the nodetest1 package,
11 error not with npm itself.
11 error Tell the author that this fails on your system:
11 error     node ./bin/www
11 error You can get their info via:
11 error     npm owner ls nodetest1
11 error There is likely additional logging output above.
12 error System Windows_NT 6.1.7601
13 error command "C:\Program Files\nodejs\\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "start"
14 error cwd C:\Users\kellington\Documents\GitHub\TEDResources\node.js\nodetest1
15 error node -v v0.10.32
16 error npm -v 1.4.28
17 error code ELIFECYCLE
18 verbose exit [ 1, true ]

//package.json

//package.json

{
  "name": "nodetest1",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "express": "~4.9.0",
    "body-parser": "~1.8.1",
    "cookie-parser": "~1.3.3",
    "morgan": "~1.3.0",
    "serve-favicon": "~2.1.3",
    "debug": "~2.0.0",
    "jade": "~1.6.0", 
    "mongodb": "*",
    "monk": "*"
  }
}

//app.js

//app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});


module.exports = app;

Microsoft Windows [Version 6.1.7601]

Copyright (c) 2009 Microsoft Corporation. All rights reserved.

版权所有 (c) 2009 Microsoft Corporation。版权所有。

//dir result

//目录结果

> [email protected] start C:\Users\username\Documents\GitHub\TEDResources\node.j
s\nodetest1
> dir

 Volume in drive C is OS
 Volume Serial Number is F87D-8771

 Directory of C:\Users\username\Desktop

10/23/2014  11:13 AM    <DIR>          .
10/23/2014  11:13 AM    <DIR>          ..
10/13/2014  09:14 AM           146,016 7f6bd778-1c67-43fd-9865-372789ed7592_Loca
lIISWebsite.zip
10/07/2014  03:39 PM             1,086 Blue Eye Macro.lnk
08/28/2014  12:07 PM             1,197 DesignEphiphany.txt
10/22/2014  08:03 AM             2,163 Git Shell.lnk
04/17/2014  10:51 AM               308 GitHub.appref-ms
10/21/2014  10:54 AM    <DIR>          LocalIISWebsite
10/23/2014  11:11 AM    <DIR>          node.js
10/14/2014  10:07 AM    <DIR>          TextDocs

               7 File(s)      1,704,717 bytes
               8 Dir(s)  352,881,205,248 bytes free

采纳答案by subpath42

Had a windows registry entry for "autorun" - "problem directory" setup for a default start directory that was overriding commands. Removed it from registry and fixed the issue. Thank you to laggingreflex for helping me debug this.

有一个用于“自动运行”的 Windows 注册表项 - 为覆盖命令的默认启动目录设置的“问题目录”。从注册表中删除它并修复了问题。感谢 laggingreflex 帮助我调试这个。

回答by farhangdon

I had this same error when I was creating my First Node Project using express. But when i tried running it using

当我使用 express 创建我的第一个节点项目时,我遇到了同样的错误。但是当我尝试使用它运行它时

node ./bin/www              // it will work

instead of

代替

npm start

The server started at port 3000. you can check your app on localhost:3000 .
May be it can help some One else :)

服务器从端口 3000 启动。您可以在 localhost:3000 上检查您的应用程序。
也许它可以帮助其他人:)

回答by Dave Munger

Fwiw, I had the same error and it turned out to be that something was already running on port 3000. I mention this because the debug log mentioned nothing about this, just a similar error to yours. So maybe this will help somebody who comes across this post and happens to have the problem I did.

Fwiw,我遇到了同样的错误,结果是某些东西已经在端口 3000 上运行了。我提到这个是因为调试日志没有提到这个,只是一个与你类似的错误。所以也许这会帮助那些遇到这篇文章并且碰巧遇到我遇到的问题的人。

回答by lacostenycoder

I had the same problem following this tutorial, only after killing server for the first time as per instructions. I tried a bunch of things, including those listed above and on several other threads. However simply removing or commenting out the following in app.js did the trick :

我在遵循本教程后遇到了同样的问题,只是在按照说明第一次杀死服务器之后。我尝试了很多东西,包括上面列出的和其他几个线程上的那些。然而,简单地删除或注释掉 app.js 中的以下内容就可以了:

//var mongo = require('mongodb')

回答by Fer B.

This happened to me today, same error looking to the same tutorial, but with this setup:

今天发生在我身上,同样的错误寻找同一个教程,但使用以下设置:

  • Ubuntu 14.04
  • npm v5.11.1
  • node v3.9.2
  • Ubuntu 14.04
  • npm v5.11.1
  • 节点 v3.9.2

First intent was searching on the web and found this page, and can't solve the problem with what was said, but found a clue to follow.

第一个意图是在网络上搜索并找到了这个页面,并且无法解决所说的问题,但找到了一个可以遵循的线索。

Second intent was to list apps using the same port (3000) with this:

第二个意图是列出使用相同端口 (3000) 的应用程序:

sudo netstat -ep | grep 3000

But nothing.

但没什么。

Third intent was to change the port number used un "nodetest1/bin/www" file from 3000to 3001, and that did the trick.

第三个意图是将“ nodetest1/bin/www”文件中使用的端口号从3000更改为3001,这就是诀窍。

var port = normalizePort(process.env.PORT || '3001');

From there, I now can browse http://localhost:3001and keep following the tutorial.

从那里,我现在可以浏览http://localhost:3001并继续遵循教程。

Edit:I've found that I've incorrectly used netstat, and this showed whats program is using the port 3000:

编辑:我发现我错误地使用了 netstat,这表明什么程序正在使用端口 3000:

sudo netstat -epa | grep 3000

ntop is using it:

ntop 正在使用它:

tcp        0      0 *:3000                  *:*                     ESCUCHAR    root       15508       2289/ntop

So in my case changing the nodetest1 port on www file was the only viable solution.

因此,在我的情况下,更改 www 文件上的 nodetest1 端口是唯一可行的解​​决方案。

Hope this help others.

希望这对其他人有帮助。

回答by Guillermo Ramirez

I had the same error, so i run:

我有同样的错误,所以我运行:

npm install

and then:

进而:

npm start

and that works for me

这对我有用

回答by tj_m

In the tutorial's app.js file, be sure to name the index object as follows:

在教程的 app.js 文件中,请务必按如下方式命名索引对象:

var index = require('./routes/index');

This should correct the error.

这应该纠正错误。

Despite what the tutorial author says, the error will throw if that line reads:

不管教程作者怎么说,如果该行显示为:

var index = require('./routes/routes');