问题
I am very new to Node.js
and 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?
I followed few instructions here to 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
回答1:
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
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)
回答2:
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.
回答3:
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 and check.
回答4:
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.
回答5:
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.
回答6:
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}!`))
来源:https://stackoverflow.com/questions/22137564/node-how-to-run-app-js