admin管理员组文章数量:1401167
Problem
NPM $npm_package_main
variable is always empty.
- When I set the package.json file with
"main": "index.js"
- Set the "start" property from scripts to
"start": "node $npm_package_main"
- Then run
npm start
Problem: the CLI executes the Node REPL mode, ignoring the "main" variable from package.json.
Expected behavior: execute the mand as node index.js
.
Environment
- Linux Ubuntu 20.04.1
npm -v
= 7.3.0node -v
= v15.5.0npm 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:
- NPM Github issue:
Problem
NPM $npm_package_main
variable is always empty.
- When I set the package.json file with
"main": "index.js"
- Set the "start" property from scripts to
"start": "node $npm_package_main"
- Then run
npm start
Problem: the CLI executes the Node REPL mode, ignoring the "main" variable from package.json.
Expected behavior: execute the mand as node index.js
.
Environment
- Linux Ubuntu 20.04.1
npm -v
= 7.3.0node -v
= v15.5.0npm 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./cli/v7/using-npm/scripts
- NPM Github issue: https://github./npm/cli/issues/2585
- Yeah, you can just set "start": "node index.js" and it works, But the "$npm_package_main" is not working. That's the point. – jotafeldmann Commented Jan 31, 2021 at 17:37
1 Answer
Reset to default 7The 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./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./npm/cli/issues/2585
- https://github./npm/cli/pull/2446
本文标签: javascriptNPM npmpackagemain variable is always emptyStack Overflow
版权声明:本文标题:javascript - NPM npm_package_main variable is always empty - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744249267a2597170.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论