简体   繁体   English

获取没有入口点的 NPM package 的路径

[英]Get a path to NPM package without entry point

A unconventional package contains a binary and nested modules but no entry point (no index script and no main ), react-scripts is an example.非常规的 package 包含二进制和嵌套模块,但没有入口点(没有索引脚本和没有main ), react-scripts就是一个例子。

require.resolve('react-scripts/bin/react-scripts') works because bin/react-scripts.js module exists but require.resolve('react-scripts') results in error because the package has no entry point. require.resolve('react-scripts/bin/react-scripts')有效,因为bin/react-scripts.js模块存在,但require.resolve('react-scripts')会导致错误,因为 package 没有入口点。

It's preferable to avoid relying on an expected location like ./node_modules/react-scripts because it depends on project structure.最好避免依赖像./node_modules/react-scripts这样的预期位置,因为它取决于项目结构。

What is a good way to get full path to react-scripts module folder?获取react-scripts模块文件夹的完整路径的好方法是什么?

It's possible to extract package path from a nearest file that is expected to exist, which is package.json :可以从预计存在的最近文件中提取 package 路径,即package.json

path.dirname(require.resolve('react-scripts/package.json'))

Still would appreciate alternative approaches to resolving a path to the the package.仍然会欣赏解决 package 路径的替代方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM