简体   繁体   English

使用全局node_modules将AngularJS与Webpack捆绑在一起

[英]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文件夹。


  • For this case, lets say my source code of the angular application is located in folder "/source/code". 对于这种情况,假设我的角应用程序的源代码位于文件夹“/ source / code”中。
  • And for the external node modules, located in "/global/npm/App1/node_module" 对于外部节点模块,位于“/ global / npm / App1 / node_module”中
  • Application name: "App1" 应用程序名称:“App1”

What I have done so far: 到目前为止我做了什么:

  1. Installed the angular application App1 with "npm install -g", so node modules will be located inside App1, in the global npm folder /global/npm/App1 使用“npm install -g”安装角度应用程序App1,因此节点模块将位于App1内,位于全局npm文件夹/ global / npm / App1中
  2. Then I try to bundle App1 from /source/code. 然后我尝试从/ source / code捆绑App1。 This won't work since I am missing node modules, which are located in /global/npm/App1/node_module. 这不起作用,因为我缺少位于/ global / npm / App1 / node_module中的节点模块。

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.

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