简体   繁体   English

NuxtJS 配置:当来自 node_modules 时,无法让插件向应用程序注册

[英]NuxtJS Configuration: Cannot get plugins to register with application when sourced from node_modules

Premise前提

I have multiple sites with similar NuxtJS based architecture and am working on generalizing code they depend on by moving it into a self-hosted npm repository.我有多个具有类似基于 NuxtJS 架构的站点,并且正在通过将它们移动到自托管的 npm 存储库来概括它们所依赖的代码。

Currently all sites have a typical nuxt directory structure in regards to plugins.目前所有站点都有一个典型的关于插件的 nuxt 目录结构。

mySite1 structure mySite1结构

/.nuxt
/assets
/components
/layouts
/middleware
/modules
/node_modules
/pages
/plugins
   myPlugin1.js
   myPlugin2.js
/static
/store
...
...

in mySite1/nuxt.config.js , I register these plugins like so:mySite1/nuxt.config.js ,我像这样注册这些插件:

...
plugins: [
   '~/plugins/myPlugin1.js',
   '~/plugins/myPlugin2.js'
]
...

example of myPlugin*.js : myPlugin*.js示例:

import Vue from 'vue'
import SomeDependency from 'some-dependency'

Vue.use(SomeDependency)

These myPlugin*.js files are duplicated in every site's (mySite1, mySite2, etc) repo, so in order to wrangle that I want to move them into my npm package (myPackage), but these plugins fail to register when I reference their path.这些myPlugin*.js文件在每个站点的(mySite1、mySite2 等)repo 中都是重复的,所以为了争论我想将它们移到我的 npm 包(myPackage)中,但是当我引用它们的路径时这些插件无法注册.

Desire:欲望:

myPackage structure myPackage结构

plugins/
   myPlugin1.js
   myPlugin2.js

mySite1/nuxt.config.js

plugins: [
   '~/node_modules/myPackage/plugins/myPlugin1.js',
   '~/node_modules/myPackage/plugins/myPlugin2.js'
]

Issue:问题:

For some reason I can't identify, none of the plugins from myPackage are being registered, and I'm looking for reasons why.出于某种原因,我无法确定, myPackage中的任何插件都没有被注册,我正在寻找原因。

A few notes:一些注意事项:

  • I actually got this to work on one site, but it failed on the next two I tried to generalize.我实际上让它在一个站点上工作,但它在接下来的两个我试图概括的站点上失败了。 This is odd, and making me think there could be some kind of race condition in nuxt's build.这很奇怪,让我觉得在 nuxt 的构建中可能存在某种竞争条件。 I am transpiling myPackage in nuxt.config.js我正在 nuxt.config.js 中myPackage nuxt.config.js
...
build: {
      transpile: [
        'myPackage'
      ],
...
  • For those that are familiar with working with self-hosted packages, when I run npm link myPackage (which symlinks to my working dir of myPackage instead of the one in node_modules ), it also works.对于那些熟悉使用自托管包的人,当我运行npm link myPackage (它符号链接到myPackage工作目录而不是node_modules中的node_modules )时,它也可以工作。

This has to either be a nuxt build thing I can't get around, or a webpack configuration thing I can't identify.这要么是我无法解决的 nuxt 构建问题,要么是我无法识别的 webpack 配置问题。 I realize this is a bit abstract, but the amount of setup it would take to make a reproduction and for anyone wanting to try this doesn't make sense.我意识到这有点抽象,但是进行复制所需的设置量以及任何想要尝试的人都没有意义。

I figured it out.我想到了。 myPackage had Vue as a dependency, as did MySite1 , MySite2 , etc. NPM will look for a dependency in the node_modules folder if the project it's currently executing code in before going up the chain, so the Vue that Vue.use was referencing was the wrong Vue . myPackageVue作为依赖项,就像MySite1MySite2等一样。 如果项目在上链之前当前正在执行代码的项目,NPM 将在node_modules文件夹中查找依赖项,因此Vue.use引用的Vue是错误的Vue

Moral of the story, only install Vue in one place.这个故事的寓意,只在一个地方安装 Vue。 In my case, that meant removing Vue as a dependency of myPackage .就我而言,这意味着删除 Vue 作为myPackage的依赖myPackage

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

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