Node - how to run app.js?

node.jsPug

node.js Problem Overview


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

node.js Solutions


Solution 1 - node.js

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)

Solution 2 - node.js

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.

Solution 3 - node.js

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": {
    ...
  }
}

Solution 4 - node.js

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.

Solution 5 - node.js

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.

Solution 6 - node.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

Solution 7 - node.js

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

Solution 8 - node.js

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

Solution 9 - node.js

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.

Solution 10 - node.js

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJunMView Question on Stackoverflow
Solution 1 - node.jsPrakash TiwariView Answer on Stackoverflow
Solution 2 - node.jsAdamView Answer on Stackoverflow
Solution 3 - node.jssimoprView Answer on Stackoverflow
Solution 4 - node.jsDamini SutharView Answer on Stackoverflow
Solution 5 - node.jsjosh3736View Answer on Stackoverflow
Solution 6 - node.jsJasberraja PView Answer on Stackoverflow
Solution 7 - node.jsAhrabprinceView Answer on Stackoverflow
Solution 8 - node.jsAnkur prajapatiView Answer on Stackoverflow
Solution 9 - node.jsSameeksha KumariView Answer on Stackoverflow
Solution 10 - node.jsAditya KumarView Answer on Stackoverflow