Reputation: 7160
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
Upvotes: 105
Views: 391274
Reputation: 1
in package.json file add the script "start":"node filename.js"
and run in terminal - > npm start
Upvotes: 0
Reputation: 1492
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.
Upvotes: 10
Reputation: 527
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
You can also see in the below image that your project runs on both command node app.js
as well as npm start
Upvotes: 5
Reputation: 2429
Assuming I have node and npm properly installed on the machine, I would
use nodemon app.js ( nodemon is a utility that will monitor for any changes in your source and automatically restart your server)
Upvotes: 166
Reputation: 13
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
Upvotes: 1
Reputation: 31
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
Upvotes: 0
Reputation: 177
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": {
...
}
}
Upvotes: 14
Reputation: 1
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}!`))
Upvotes: -4
Reputation: 1264
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.
Upvotes: -2
Reputation: 145002
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.
Upvotes: 2
Reputation: 1099
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.
Upvotes: 21