[英]Bundling AngularJS with Webpack, using global node_modules
What I want too archieve: Bundle angularjs application with webpack, but refer to node_modules folder located in a different location on the same filesystem. 我想要的太多了:使用webpack捆绑angularjs应用程序,但是请参阅位于同一文件系统上不同位置的node_modules文件夹。
What I have done so far: 到目前为止我做了什么:
How can I refer to the node modules located in /global/npm/App1/node_module? 如何引用位于/ global / npm / App1 / node_module中的节点模块? Do I have to use absolut path for each module I require in webpack.config.js? 我是否必须在webpack.config.js中为每个模块使用绝对路径?
I think the feature within Webpack you are looking for is the resolve-modules
. 我认为您正在寻找的Webpack中的功能是resolve-modules
。 This allows you to add additional folders that can be used without the need to add a path into them, almost acting like they are within the node_modules
folder. 这允许您添加可以使用的其他文件夹,而无需在其中添加路径,几乎就像它们在node_modules
文件夹中一样。
https://webpack.js.org/configuration/resolve/#resolve-modules https://webpack.js.org/configuration/resolve/#resolve-modules
For example 例如
const globalNpm = path.join(__dirname, 'global', 'npm', 'App1', 'node_module'),
resolve: {
modules: [
globalNpm
]
}
I find the using path.join make your webpack code more likely to work across platforms (windows, mac, etc). 我发现使用path.join使你的webpack代码更有可能跨平台(windows,mac等)工作。 The path in the above example may be wrong as I could really get your folder structure from your question. 上面示例中的路径可能有误,因为我可以从您的问题中获得您的文件夹结构。 But the above should allow you require modules directly from your global npm folder. 但上面应该允许您直接从全局npm文件夹中获取模块。
Please the the above feature is mostly intended for large project and files that could be moved, or reused elsewhere, it stops the need to have 请将上述功能主要用于大型项目和可以移动或在其他地方重复使用的文件,这样就不需要了
const module = require('../../../a-folder/../../fancycomponent')
On a side note - installing NPM packages globally to be used just within one app is not a normal pattern and can lead to problems, especially with permissions and versioning on local and CI/CD pipelines. 另一方面 - 全局安装NPM软件包仅在一个应用程序中使用不是正常模式,可能导致问题,尤其是本地和CI / CD管道上的权限和版本控制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.