node.js Node - 如何运行 app.js?

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

Node - how to run app.js?

node.jspug

提问by JunM

I am very new to Node.jsand I tried to run a project (made by other developer) by having a command in terminal node app.js. But I encountered below error, do you have any idea how to run this project?

我很新Node.js,我试图通过在终端中运行一个命令来运行一个项目(由其他开发人员制作)node app.js。但是我遇到了下面的错误,你知道如何运行这个项目吗?

I followed few instructions hereto run a project.

在这里遵循了一些说明来运行一个项目。

Error logs below:

错误日志如下:

Junryls-Mac-mini:app junrylmaraviles$ node app.js

/Users/junrylmaraviles/Desktop/myfolder/mysubfolder/app/app.js:1
(function (exports, require, module, __filename, __dirname) { define('src/app'
                                                              ^
ReferenceError: define is not defined
    at Object.<anonymous> (/Users/junrylmaraviles/Desktop/myfolder/mysubfolder/app/app.js:1:63)
    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)
    at startup (node.js:119:16)
    at node.js:902:3

回答by Prakash Tiwari

Assuming I have nodeand npmproperly installed on the machine, I would

假设我在机器上正确安装了nodenpm,我会

  • Download the code
  • Navigate to inside the project folder on terminal, where I would hopefully see a package.json file
  • Do an npm installfor installing all the project dependencies
  • Do an npm install -g nodemonfor installing all the project dependencies
  • Then npm startOR node app.jsOR nodemon app.jsto get the app running on local host
  • 下载代码
  • 导航到终端上的项目文件夹内,我希望在那里看到 package.json 文件
  • 执行npm install以安装所有项目依赖项
  • 执行npm install -g nodemon以安装所有项目依赖项
  • 然后npm startOR node app.jsOR nodemon app.js让应用在本地主机上运行

Hope this helps someone

希望这有助于某人

use nodemon app.js( nodemon is a utility that will monitor for any changes in your source and automatically restart your server)

使用nodemon app.js( nodemon 是一个实用程序,它将监视源中的任何更改并自动重新启动服务器)

回答by Adam

The code downloaded may require you to install dependencies first. Try commands(in app.js directory): npm installthen node app.js. This should install dependencies and then start the app.

下载的代码可能需要您先安装依赖项。尝试命令(在 app.js 目录中):npm installthen node app.js。这应该安装依赖项,然后启动应用程序。

回答by Damini Suthar

To run app.js file check "main": "app.js"in your package.json file.

要运行 app.js 文件,请检查"main": "app.js"您的 package.json 文件。

Then run command $ node app.jsThat should run your app and check.

然后运行$ node app.js应该运行您的应用程序并检查的命令。

回答by josh3736

Node is complaining because there is no function called define, which your code tries to call on its very first line.

Node 抱怨是因为没有名为 的函数define,您的代码试图在其第一行调用该函数。

definecomes from AMD, which is not used in standard node development.

define来自AMD,它不用于标准节点开发。

It is possible that the developer you got your project from used some kind of trickeryto use AMD in node. You should ask this person what special steps are necessary to run the code.

您获得项目的开发人员可能使用某种技巧在node.js中使用 AMD。您应该询问此人运行代码需要哪些特殊步骤。

回答by simopr

Just adding this. In your package.json, if your "main": "index.js"is correctly set. Just use node .

加了这个就行了。在您的 package.json 中,如果您的"main": "index.js"设置正确。只需使用node 。

{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
     ...
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
     ...
  },
  "devDependencies": {
    ...
  }
}

回答by Sameeksha Kumari

Node manages dependencies ie; third party code using package.json so that 3rd party modules names and versions can be kept stable for all installs of the project. This also helps keep the file be light-weight as only actual program code is present in the code repository. Whenever repository is cloned, for it to work(as 3rd party modules may be used in the code), you would need to install all dependencies. Use npm installon CMD within root of the project structure to complete installing all dependencies. This should resolve all dependencies issues if dependencies get properly installed.

节点管理依赖项,即;使用 package.json 的第三方代码,以便 3rd 方模块名称和版本可以在项目的所有安装中保持稳定。这也有助于保持文件的轻量级,因为代码存储库中只存在实际的程序代码。每当克隆存储库时,要使其工作(因为代码中可能会使用 3rd 方模块),您需要安装所有依赖项。npm install在项目结构根目录中的 CMD 上使用以完成所有依赖项的安装。如果正确安装了依赖项,这应该可以解决所有依赖项问题。

回答by Aditya Kumar

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(`Example app listening on port ${port}!`))