node.js 节点。并且 npm start 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24334005/
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 17:31:04  来源:igfitidea点击:

node . and npm start don't work

node.jsexpressnpm

提问by Bren

The title says most of the problem. When I try to run node .I get:

标题说明了大部分问题。当我尝试运行时,node .我得到:

    module.js:340
    throw err;
          ^
Error: Cannot find module 'static-favicon'
    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:902:3

There seems to be no modules folder actually. I'm just running expressin an empty directory

实际上似乎没有模块文件夹。我只是express在一个空目录中运行

npm works fine however. This is with a fresh express install if it matters. Any help would be awesome, thanks!

但是 npm 工作正常。如果重要的话,这是一个全新的快速安装。任何帮助都会很棒,谢谢!

the full error messages:

完整的错误信息:

 new-host-2:~ Brennan$ cd Desktop/
new-host-2:Desktop Brennan$ mkdir test4
new-host-2:Desktop Brennan$ cd test4
new-host-2:test4 Brennan$ express -e

   create : .
   create : ./package.json
   create : ./app.js
   create : ./public
   create : ./public/javascripts
   create : ./public/images
   create : ./public/stylesheets
   create : ./public/stylesheets/style.css
   create : ./routes
   create : ./routes/index.js
   create : ./routes/users.js
   create : ./views
   create : ./views/index.ejs
   create : ./views/error.ejs
   create : ./bin
   create : ./bin/www

   install dependencies:
     $ cd . && npm install

   run the app:
     $ DEBUG=test4 ./bin/www

new-host-2:test4 Brennan$ node app.js

module.js:340
    throw err;
          ^
Error: Cannot find module 'static-favicon'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/Users/Brennan/Desktop/test4/app.js:3:15)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
new-host-2:test4 Brennan$ npm start app.js
npm ERR! Error: ENOENT, open '/Users/Brennan/Desktop/test4/node_modules/app.js/package.json'
npm ERR! If you need help, you may report this *entire* log,
npm ERR! including the npm and node versions, at:
npm ERR!     <http://github.com/npm/npm/issues>

npm ERR! System Darwin 12.4.0
npm ERR! command "node" "/usr/local/bin/npm" "start" "app.js"
npm ERR! cwd /Users/Brennan/Desktop/test4
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.7
npm ERR! path /Users/Brennan/Desktop/test4/node_modules/app.js/package.json
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/Brennan/Desktop/test4/npm-debug.log
npm ERR! not ok code 0
new-host-2:test4 Brennan$ forever app.js
warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms

module.js:340
    throw err;
          ^
Error: Cannot find module 'static-favicon'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/Users/Brennan/Desktop/test4/app.js:3:15)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
error: Forever detected script exited with code: 8

回答by dylants

After using the express-generatorto generate the node application, you need to install the dependencies for the project. This is done via:

使用express-generator生成节点应用后,需要为项目安装依赖。这是通过以下方式完成的:

$ npm install

Once this is done, you can start the app using npm:

完成此操作后,您可以使用npm以下命令启动应用程序:

$ npm start

By default, the express generated apps state this as the startcommand for npm(you can view this in the package.jsonfile):

默认情况下,express 生成的应用程序将此声明为以下start命令npm(您可以在package.json文件中查看):

"start": "node ./bin/www"

So to execute the same thing via the command line, you would run:

因此,要通过命令行执行相同的操作,您可以运行:

$ node ./bin/www

回答by Jan Michael Auer

I had the same issue after running the express-generator on a non-empty directory with some node modules already installed in node_modules, especially express itself. Simply nuke the folder, reinstall all your dependencies and you should be good to go:

在已经安装了一些节点模块的非空目录上运行 express-generator 后,我遇到了同样的问题node_modules,尤其是 express 本身。只需核对文件夹,重新安装所有依赖项,您就可以开始了:

rm -rf ./node_modules
npm install
npm start

EDIT:Tuns out that at some stage in the process I had installed serve-faviconand saved that to the local package.json. Looks like the express generator failed to add that dependency. Therefore:

编辑:在我安装serve-favicon并将其保存到本地 package.json的过程中的某个阶段调出。看起来快速生成器未能添加该依赖项。所以:

npm install serve-favicon --save
npm start

回答by Dan

given a dir tree like:

给定一个目录树,如:

myapp
 - lib
  - favicon.ico
 - app.js
 - package.json
 - node_modules (npm installs all modules in here)

if you are trying to access favicon.ico in your app.js

如果您尝试在 app.js 中访问 favicon.ico

var express = require('express'),
    fs = require('fs'); // this is for reading static files 

var favicon = require(fs.readFileSync('./lib/favicon.ico'));

the var for favicon may not be what you expect. The best way for serving static files would be to set up a static directory with a route so anyone hitting that route with a param gets that files: for example:

favicon 的 var 可能不是您所期望的。提供静态文件的最佳方法是设置一个带有路由的静态目录,以便任何使用参数访问该路由的人都可以获取该文件:例如:

https://mynodeapp.com/static/images/{param}

where param would be the name of the file (this is an example using Hapi - express has something similiar for serving static files)

其中 param 将是文件的名称(这是一个使用 Hapi 的示例 - express 有一些类似的东西可以用于提供静态文件)

回答by user4447835

Use serve-favicon instead of static-favicon. Also verify that you have serve-favicon installed correctly. Do cd serve-favicon, check package.json, and then run npm install to have all dependencies installed correctly.

使用 serve-favicon 而不是 static-favicon。还要验证您是否正确安装了服务网站图标。执行 cd serve-favicon,检查 package.json,然后运行 ​​npm install 以正确安装所有依赖项。

回答by santhosh

Do npmstartwhich installs all the required modules for your project

执行npmstart它为您的项目安装所有必需的模块