96

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
Community
  • 1
  • 1
JunM
  • 7,040
  • 7
  • 37
  • 58
  • When I start using a new technology, I start downloading samples from the [website](http://nodejs.org/), read blogs and [visit the community](http://nodejs.org/community/). – Uwe Keim Mar 03 '14 at 05:22
  • 1
    @UweKeim Thanks, that would be a good start for me. Still doing it though and maybe I could have this figured out along the way :) – JunM Mar 03 '14 at 05:30
  • check the package.json file and see if it contains a script line. You may need to run more than one file, which hopefully could be done with something like `npm start`. – Jake Sellers Mar 03 '14 at 05:34
  • First result from google is this [SO post](http://stackoverflow.com/questions/9303945/why-do-i-see-define-not-defined-when-running-a-mocha-test-with-requirejs). Skimmed it, you need to add some boilerplate bla bla bla, at least try google next time... – Jake Sellers Mar 03 '14 at 05:37
  • Which part of the other pages instructions are you following? What version of node are you using `node --version`? Whats the download link for the other peoples code? It seems to me that define() was available in node 0.5.0 which was 5 minor versions ago and the api has changed since then. If the code is really old try finding a newer example or tutorial to follow. – Adam Mar 04 '14 at 05:26
  • @Adam - this is now working, I've asked helped from the developer of the project. Thanks. – JunM Mar 04 '14 at 05:29
  • You should answer the question yourself with the information provided for posterity. – Adam Mar 04 '14 at 05:31
  • @Adam I should say it is not really running app.js that is working. It is another way around. But I still don't know to run the app.js and I no longer have any interest on running it unless if needed. – JunM Mar 04 '14 at 05:41
  • 2 years later, can't help but notice that "define is undefined" is a paradox ;) – TheGenie OfTruth Feb 24 '17 at 01:19

11 Answers11

159

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)

Halo
  • 1,730
  • 1
  • 8
  • 31
Prakash Tiwari
  • 2,351
  • 1
  • 20
  • 13
  • @Prakash Tiwari thanks a lot, I'm learning from Udemy and the course use C9 and I was not finding how to run a proyect on localhost! – fedeteka Nov 06 '18 at 18:42
  • 2
    Plus 1 for `nodemon app.js` – Jayram Kumar Apr 05 '19 at 04:21
  • going to localhost:8080 doesn't work today./sad face – Urasquirrel May 02 '19 at 01:54
  • 1
    Damn, it's been almost 8 years till I accepted this answer. Such a newbie question I had here :) – JunM Aug 02 '21 at 02:56
  • There is also one more hack that I would like to add up, we can also make a script that will run our specific cmd, also open browser(localhost), e.g for my typescript node app I have added the below script in package.json file ```"starto": "cd src && tsc && open http://localhost:3000 && node server.js"```, to run it use-> ```npm run starto``` – Wasit Shafi Dec 10 '21 at 07:36
21

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.

Adam
  • 1,059
  • 9
  • 15
  • 3
    `npm install` did some successful installation but when I ran `node app.js` I still encounter the same error. – JunM Mar 03 '14 at 02:33
12

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": {
    ...
  }
}
simopr
  • 159
  • 1
  • 7
11

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.

Damini Suthar
  • 1,470
  • 2
  • 14
  • 43
3

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

Ankur prajapati
  • 485
  • 6
  • 9
2

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.

josh3736
  • 139,160
  • 33
  • 216
  • 263
1

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
Jasberraja P
  • 13
  • 1
  • 6
0

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

0

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

and run in terminal - > npm start

-2

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.

Sameeksha Kumari
  • 1,158
  • 2
  • 16
  • 21
-4
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}!`))