How do I make "node ." work? - node.js

I've run node . in various NodeJS apps in the past, and it seems to know to run index.js, server.js, etc. Is there a package.json setting (or something similar) that I can configure so that NodeJS knows which file should run?

npm does have a one configuration for that:
"main": "app.js"

Related

How to make a node.js server run with a specific command

I want to start my index.js file with node --max-old-space-size=1024 index.js but I don't want to type the command manually everytime, any Idea how can I make a .js file that executes the command for me? Like start.js and when I launch it, it immediately runs node --max-old-space-size=1024 index.js. This might be super straight forward but I'm still new to node.js 😅
You can modify your package.json to create a command that execute node --max-old-space-size=1024 index.js
In your package.json
"scripts": {
"start": "node --max-old-space-size=1024 index.js"
}
And then run npm run start.

Convert ts-node package.json script to nodejs package.json script when building

I have a TypeScript project with this structure:
root
src
index.ts
package.json
dist
index.js
package.json
This will be hosted in Azure Web Apps. Currently I have my start script more or less like this:
"start": "ts-node index.ts"
I would like build to dist/ folder and have the script be:
"start": "node index.js"
As I will only be deploying contents of dist/. Grunt is used to build/copy (I've found this package called grunt-replace but I don't know if that's a good approach).
Is there any way to achieve this, or do I need to restructure dist/? The only other production TypeScript/Node.js project I've worked on is an Azure Function which leverages azure-functions-core-tools' func host start from dist/.

How to run a node app in another machine without nodemon installed as global

I am building an app using nodejs and express framework and have installed nodemon globally to restart the server automatically.
Now if I take this app and run in another machine/host/environment (without nodemon installed globally) then it would produce an error because package.json doesn't contain nodemon as a dependency.
What would be the best way to fix this?
Include nodemon in your package.json as a dev dependency. So it will be available in the context of the script.
{
"scripts": {
"start": "nodemon my_file.js"
},
"devDependencies": {
"nodemon": "<version>",
}
}
Then from the terminal you can use npm start
Nodemon has been the goto for a long time, but now with Node.js 19, we have --watch flag, which restarts server when files changes. Keep in mind this is experimental
Docs
:)

Create single binary from express + react app with zeit pkg?

How do I package an express + react folderstructure into a single binary with zeit?
I can run each of them like this:
node server.js
npm run start client
I can start both like this:
scripts element in package.json:
"myapp": "concurrently --kill-others \"node server\" \"npm run --prefix client start\"",
then - npm run myapp in the same folder as package.json.
What I would like to achieve is to apply zeit/pkg somehow so that I have a single binary that I can run that starts both servers in the same way as npm run myapp.
Anyone who knows how?
I don't think zeit/pkg accepts multiple entry points based on their documentation
The entrypoint of your project is a mandatory CLI argument. It may be:
Path to entry file. Suppose it is /path/app.js, then packaged app will work the same way as node /path/app.js
Path to package.json. Pkg will follow bin property of the specified package.json and use it as entry file.
Path to directory. Pkg will look for package.json in the specified directory. See above.
Maybe the better route would be to do some server-side rendering via their Next.js framework? Then you would only have to package your app via the NodeJS entry point.

Node - how to run app.js?

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
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)
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.
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": {
...
}
}
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 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.
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
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
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
in package.json file add the script
"start":"node filename.js"
and run in terminal - > npm start
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}!`))

Resources