Skip to main content

🚀 What is NPM?

NPM (Node Package Manager) is the default package manager for Node.js, used to install libraries and dependencies. Additionally, you can use it to run your application's startup scripts.

warning

Although NPM allows running scripts with npm start, it is not a process manager. This means it does not provide fault tolerance, automatic application restart in case of crash or failure, and does not keep the process running in the background. For production environments, it is recommended to use a process manager like PM2. To learn about PM2, click here!.

🛠️ How to use NPM on SaveinCloud

On the platform, you can integrate NPM with your Node.js application in two main ways:

1️⃣ - Selecting the "NPM" Tag When Creating the Environment

When creating a new environment on SaveinCloud, you can choose a pre-configured image that already includes Node.js and NPM installed. This option speeds up the deployment process, allowing your application to be managed by NPM from the start.

NPM Topologia

2️⃣ - Configuring NPM via Environment Variables

If you already have a running environment, you can configure NPM using environment variables.

Variáveis de Ambiente - Parte 1

Variáveis de Ambiente - Parte 2

📄 Configuration of the package.json File

For NPM to run your application correctly, it is necessary to have the startup script inside the configuration file called package.json, which must be located in the /home/jelastic/ROOT directory - the default defined in the environment variable $ROOT_DIR, although the directory can be changed as needed -. This file defines how NPM should start your application.

info

The platform will look for standard entrypoint files - such as server.js, index.js, app.js - inside the /home/jelastic/ROOT directory (in this case, the directory specified in the environment variable $ROOT_DIR) to automatically start your application. If your application's entrypoint has a different name, you can specify it in the environment variable $APP_FILE.

Example of a startup script for the application in the package.json file:

{
"scripts": {
"start": "node ./server.js"
}
}
  • start: is the application's entry point and will be executed with npm start.
warning

🔒 Make sure to open the firewall port your application uses to ensure proper operation. To learn about Firewall Management, click here!

🧰 Useful NPM Commands

CommandDescription
npm installInstalls all dependencies listed in package.json.
npm install <pacote>Installs a specific package locally.
npm run startRuns the script defined as start in package.json.
npm run <nome-script>Runs any other custom defined script.
npm outdatedChecks for outdated dependencies.
npm updateUpdates all dependencies to the latest allowed version.
info

💡 Remember to store your package.json file inside the /home/jelastic/ROOT directory, as the platform will execute the command npm install to install the dependencies contained in your file at each instance restart (the environment variable $UPDATE_PACKAGES_ON_RESTART must have the value true).

🧠 Questions?

Contact technical support and send your question; we are available to help you!