node.js运行npm失败

事情是这样的,我在自学node.js,照着书上的实例,我执行这一步

express -e ejs myapp

然后按照提示执行这一步

$ cd myapp && npm install

但是总是不能成功,错误提示是这样的(此处已修改,我将报错开始以后的地方都贴出来了)

npm ERR! Error: EACCES, open '/Users/cs/.npm/parseurl/1.0.1/package.tgz'
npm ERR! { [Error: EACCES, open '/Users/cs/.npm/parseurl/1.0.1/package.tgz']
npm ERR! errno: 3,
npm ERR! code: 'EACCES',
npm ERR! path: '/Users/cs/.npm/parseurl/1.0.1/package.tgz',
npm ERR! parent: 'express' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! System Darwin 13.2.0
npm ERR! command "node" "/usr/local/bin/npm" "install"
npm ERR! cwd /Users/cs/myapp
npm ERR! node -v v0.10.28
npm ERR! npm -v 1.4.9
npm ERR! path /Users/cs/.npm/parseurl/1.0.1/package.tgz
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, open '/Users/cs/.npm/parseurl/1.0.1/package.tgz'
npm http 200 http://registry.cnpmjs.org/methods/download/methods-1.0.0.tgz
npm ERR! fetch failed http://registry.cnpmjs.org/utils-merge/download/utils-merge-1.0.0.tgz
npm ERR! fetch failed http://registry.cnpmjs.org/send/download/send-0.3.0.tgz
npm ERR! fetch failed http://registry.cnpmjs.org/merge-descriptors/download/merge-descriptors-0.0.2.tgz
npm http 200 http://registry.cnpmjs.org/cookie/download/cookie-0.1.2.tgz
npm http 200 http://registry.cnpmjs.org/escape-html/download/escape-html-1.0.1.tgz
npm http 200 http://registry.cnpmjs.org/qs/download/qs-0.6.6.tgz
npm http 200 http://registry.cnpmjs.org/path-to-regexp/download/path-to-regexp-0.1.2.tgz
npm http 200 http://registry.cnpmjs.org/serve-static/download/serve-static-1.1.0.tgz
npm http GET http://registry.cnpmjs.org/utils-merge/download/utils-merge-1.0.0.tgz
npm http GET http://registry.cnpmjs.org/send/download/send-0.3.0.tgz
npm http GET http://registry.cnpmjs.org/merge-descriptors/download/merge-descriptors-0.0.2.tgz
npm http 200 http://registry.cnpmjs.org/utils-merge/download/utils-merge-1.0.0.tgz
npm http 200 http://registry.cnpmjs.org/send/download/send-0.3.0.tgz
npm http 200 http://registry.cnpmjs.org/merge-descriptors/download/merge-descriptors-0.0.2.tgz
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/cs/myapp/npm-debug.log
npm ERR! not ok code 0
chenshengtekiMacBook-Pro:myapp cs$
希望高手能解答一下,谢谢了


好吧,评论里面有人让我贴一下npm-debug.log的代码,但是我不是很懂,我贴一下吧(不全,问题字数有限制),如下:


第三次修改,采纳评论建议,贴出npm-debug.log最后几行代码,如下:

1120 verbose lock tar:///Users/cs/.npm/range-parser/1.0.0/package /Users/cs/.npm/6b119fe2-s-npm-range-parser-1-0-0-package.lock
1121 silly lockFile f6cc1fb8-96861-0-2912775008007884-tmp-tgz tar:///var/folders/n6/6gfz_ysn65v0wn_b0xr9r2sr0000gn/T/npm-839-HO1LYWuD/1409886696861-0.2912775008007884/tmp.tgz
1122 verbose lock tar:///var/folders/n6/6gfz_ysn65v0wn_b0xr9r2sr0000gn/T/npm-839-HO1LYWuD/1409886696861-0.2912775008007884/tmp.tgz /Users/cs/.npm/f6cc1fb8-96861-0-2912775008007884-tmp-tgz.lock
1123 silly lockFile 9cf60328--download-serve-static-1-1-0-tgz http://registry.cnpmjs.org/serve-static/download/serve-static-1.1.0.tgz
1124 verbose lock http://registry.cnpmjs.org/serve-static/download/serve-static-1.1.0.tgz /Users/cs/.npm/9cf60328--download-serve-static-1-1-0-tgz.lock
1125 verbose addRemoteTarball [ 'http://registry.cnpmjs.org/serve-static/download/serve-static-1.1.0.tgz',
1125 verbose addRemoteTarball '454dfa05bb3ddd4e701a8915b83a278aa91c5643' ]
1126 info retry fetch attempt 1 at 11:11:37
1127 verbose fetch to= /var/folders/n6/6gfz_ysn65v0wn_b0xr9r2sr0000gn/T/npm-839-HO1LYWuD/1409886697399-0.13988921837881207/tmp.tgz
1128 http GET http://registry.cnpmjs.org/serve-static/download/serve-static-1.1.0.tgz
1129 silly gunzTarPerm modes [ '755', '644' ]
1130 silly gunzTarPerm extractEntry package.json
1131 silly gunzTarPerm extractEntry .npmignore
1132 silly gunzTarPerm extractEntry index.js
1133 silly gunzTarPerm extractEntry History.md
1134 silly gunzTarPerm extractEntry Makefile
1135 silly gunzTarPerm extractEntry Readme.md
1136 silly lockFile 6b119fe2-s-npm-range-parser-1-0-0-package tar:///Users/cs/.npm/range-parser/1.0.0/package
1137 silly lockFile 6b119fe2-s-npm-range-parser-1-0-0-package tar:///Users/cs/.npm/range-parser/1.0.0/package
1138 silly lockFile f6cc1fb8-96861-0-2912775008007884-tmp-tgz tar:///var/folders/n6/6gfz_ysn65v0wn_b0xr9r2sr0000gn/T/npm-839-HO1LYWuD/1409886696861-0.2912775008007884/tmp.tgz
1139 silly lockFile f6cc1fb8-96861-0-2912775008007884-tmp-tgz tar:///var/folders/n6/6gfz_ysn65v0wn_b0xr9r2sr0000gn/T/npm-839-HO1LYWuD/1409886696861-0.2912775008007884/tmp.tgz
1140 silly lockFile 2d1466df--download-range-parser-1-0-0-tgz http://registry.cnpmjs.org/range-parser/download/range-parser-1.0.0.tgz
1141 silly lockFile 2d1466df--download-range-parser-1-0-0-tgz http://registry.cnpmjs.org/range-parser/download/range-parser-1.0.0.tgz
1142 silly lockFile ed7aad53-range-parser-1-0-0 range-parser@1.0.0
1143 silly lockFile ed7aad53-range-parser-1-0-0 range-parser@1.0.0
1144 verbose about to build /Users/cs/myapp/node_modules/express
1145 info /Users/cs/myapp/node_modules/express unbuild
1146 info preuninstall express@4.2.0
1147 info uninstall express@4.2.0
1148 verbose true,/Users/cs/myapp/node_modules,/Users/cs/myapp/node_modules unbuild express@4.2.0
1149 info postuninstall express@4.2.0
1150 error Error: EACCES, open '/Users/cs/.npm/range-parser/1.0.0/package.tgz'
1150 error { [Error: EACCES, open '/Users/cs/.npm/range-parser/1.0.0/package.tgz']
1150 error errno: 3,
1150 error code: 'EACCES',
1150 error path: '/Users/cs/.npm/range-parser/1.0.0/package.tgz',
1150 error parent: 'express' }
1151 error Please try running this command again as root/Administrator.
1152 error System Darwin 13.2.0
1153 error command "node" "/usr/local/bin/npm" "install"
1154 error cwd /Users/cs/myapp
1155 error node -v v0.10.28
1156 error npm -v 1.4.9
1157 error path /Users/cs/.npm/range-parser/1.0.0/package.tgz
1158 error code EACCES
1159 error errno 3
1160 error stack Error: EACCES, open '/Users/cs/.npm/range-parser/1.0.0/package.tgz'
1161 verbose exit [ 3, true ]


新问题:我现在碰到了一个新的问题,就是我现在npm install运行成功了,但是我执行项目里的app.js却执行不起来。不知道是什么问题?

debug@0.7.4 node_modules/debug

static-favicon@1.0.2 node_modules/static-favicon

ejs@0.8.8 node_modules/ejs

morgan@1.0.1 node_modules/morgan └── bytes@0.3.0

cookie-parser@1.0.1 node_modules/cookie-parser ├──
cookie-signature@1.0.3 └── cookie@0.1.0

body-parser@1.0.2 node_modules/body-parser ├── qs@0.6.6 ├──
type-is@1.1.0 (mime@1.2.11) └── raw-body@1.1.7
(string_decoder@0.10.31, bytes@1.0.0) express@4.2.0
node_modules/express ├── parseurl@1.0.1 ├── utils-merge@1.0.0 ├──
merge-descriptors@0.0.2 ├── cookie@0.1.2 ├── escape-html@1.0.1 ├──
debug@0.8.1 ├── cookie-signature@1.0.3 ├── fresh@0.2.2 ├──
range-parser@1.0.0 ├── qs@0.6.6 ├── methods@1.0.0 ├──
buffer-crc32@0.2.1 ├── serve-static@1.1.0 ├── path-to-regexp@0.1.2 ├──
type-is@1.1.0 (mime@1.2.11) ├── send@0.3.0 (debug@0.8.0, mime@1.2.11)
└── accepts@1.0.1 (negotiator@0.4.7, mime@1.2.11)
chenshengtekiMacBook-Pro:myapp cs$ node app.js
chenshengtekiMacBook-Pro:myapp cs$

我执行以后又回到chenshengtekiMacBook-Pro:myapp cs$这里,没法往下执行了

阅读 24.8k
3 个回答

microblog的入口不是 app.js 这个项目用npm管理的

你看 package.json 中 启动命令行是
node ./bin/www
入口是这个文件 监听端口和初始化代码也在这个文件里

用npm管理的项目 启动不用执行指定js文件 直接无脑 npm start 就可以了

卧槽,我最后提的那个问题居然被我这个菜鸟解决了,但绝对没科学道理,我说一下,我看了用我的环境生成的app.js

var express = require('express');
var path = require('path');
var favicon = require('static-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', 'ejs');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
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;

-----------------------------------分割线------------------------------------

然后是书上案例的app.js,是这个样的

/**

Module dependencies.
*/
var express = require('express')
  , routes = require('./routes');

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
  app.set('views', dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

// Routes

app.get('/', routes.index);

app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

我发现我的app.js少一个“app.listen(3000);”,于是我手动加了一个到我的app.js里,然后就跑起来了,但是我不知道,为什么我的app.js里没有这句话,求大神解释,相关参数如下:
系统:Macosx
node版本:v0.10.28
express版本:4.2.0
npm版本:1.4.9

因为在bin目录下的www文件已经有listen这句话了。正常用npm start启动的话是启动的www这个文件。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏