NPM $npm_package_main variable is always empty.
"main": "index.js""start": "node $npm_package_main"npm startProblem: the CLI executes the Node REPL mode, ignoring the "main" variable from package.json.
Expected behavior: execute the command as node index.js.
npm -v = 7.3.0node -v = v15.5.0npm run env | grep npm_package_name = npm_package_name=appnpm run env | grep npm_package_main = EMPTYconsole.log('HELLO');npm init and hit ENTER for all questionspackage.json file and add the following line to the "scripts" property:
"start": "node $npm_package_main", {
"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"
}
Set "start" and running "npm start" for:
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
{
"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"
}