Node-如何运行 app.js?

我是非常新的 Node.js和我试图运行一个项目(由其他开发人员)有一个命令在终端 node app.js。但我遇到以下错误,你有任何想法如何运行这个项目?

我遵循 给你的几条指令来运行一个项目。

错误日志如下:

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
304186 次浏览

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

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

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

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

Assuming I have node and npm properly installed on the machine, I would

  • Download the code
  • Navigate to inside the project folder on terminal, where I would hopefully see a package.json file
  • Do an npm install for installing all the project dependencies
  • Do an npm install -g nodemon for installing all the project dependencies
  • Then npm start OR node app.js OR nodemon app.js to get the app running on local host

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

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

Then run command $ node app.js That should run your app.

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 install on CMD within root of the project structure to complete installing all dependencies. This should resolve all dependencies issues if dependencies get properly installed.

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}!`))

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

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

you have a package.json file that shows the main configuration of your project, and a lockfile that contains the full details of your project configuration such as the urls that holds each of the package or libraries used in your project at the root folder of the project......

npm is the default package manager for Node.js.... All you need to do is call $ npm install from the terminal in the root directory where you have the package.json and lock file ...since you are not adding any particular package to be install ..... it will go through the lock file and download one after the other, the required packages from their urls written in the lock file if it isnt present in the project enviroment .....

you make sure you edit your package.json file .... to give an entry point to your app..... "name":"app.js" where app.js is the main script .. or index.js depending on the project naming convention...

then you can run..$ Node app.js or $ npm start if your package.json scripts has a start field config as such "scripts": { "start": "Node index.js", "test": "test" }..... which is indirectly still calling your $ Node app.js

If the Node Js Project :

           Normally we can run,
>node app


(or)


Install nodemon dependency (npm i -g nodemon)
>nodemon app.js


(or)


In Package.json, inside the scripts has "start":"nodemon app.js"
>npm start

To run a node js project you can run the project by below commands

node app.js

But if you want to run your project with npm start then you need to pass "start": "Node app.js" in the scripts of the package.json file

So, your package.json file will look like below

"scripts": { "start": "node app.js", "test": "test" }

Once you are done with the changes then you just need to save the file and then go to the terminal hit the npm start command you will see that the project started as its working on the node app.js command

Refer to below image for clarification

enter image description here

You can also see in the below image that your project runs on both command node app.js as well as npm start

enter image description here

in package.json file add the script "start":"node filename.js"

and run in terminal - > npm start