Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
NPM npm_package_main variable is always empty

Problem

NPM $npm_package_main variable is always empty.

  1. When I set the package.json file with "main": "index.js"
  2. Set the "start" property from scripts to "start": "node $npm_package_main"
  3. Then run npm start

Problem: the CLI executes the Node REPL mode, ignoring the "main" variable from package.json.

Expected behavior: execute the command as node index.js.

Environment

  • Linux Ubuntu 20.04.1
  • npm -v = 7.3.0
  • node -v = v15.5.0
  • npm run env | grep npm_package_name = npm_package_name=app
  • npm run env | grep npm_package_main = EMPTY

How to reproduce

  • Create an "app" directory and enter the new directory
  • Create an "index.js" file with the following content console.log('HELLO');
  • Run npm init and hit ENTER for all questions
  • Edit the package.json file and add the following line to the "scripts" property: "start": "node $npm_package_main",
  • now your package.json must look like this
    {
      "name": "app",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "devDependencies": {},
      "scripts": {
        "start": "node $npm_package_main",
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC"
    }
  • Run "npm start"
  • File "index.js" is not executed and Node enters the REPL mode.

Attempts

Set "start" and running "npm start" for:

  • "echo $npm_package_main" prints nothing
  • "echo $npm_package_name" prints "app"
  • "echo $npm_package_version" prints "1.0.0"

References

  • NPM package.json variables: https://docs.npmjs.com/cli/v7/using-npm/scripts
  • NPM Github issue: https://github.com/npm/cli/issues/2585
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

  • The official answer from NPM: use "node .". There's no official reason for this behavior, until this post.

  • According to the documentation, the "main" property contains the entrypoint for your app when is used as a module in other projects: https://docs.npmjs.com/cli/v7/configuring-npm/package-json#main

Solution

  • Use the "config" property instead:
{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "devDependencies": {},
  "config": {
    "main": "index.js"
  },    
  "scripts": {
    "start": "node $npm_package_config_main",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}
  • Run "npm start"
  • It works

References:

  • https://github.com/npm/cli/issues/2585
  • https://github.com/npm/cli/pull/2446
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda