[英]NuxtJS Configuration: Cannot get plugins to register with application when sourced from node_modules
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)中,但是当我引用它们的路径时这些插件无法注册.
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'
]
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:一些注意事项:
myPackage
in nuxt.config.js
我正在 nuxt.config.js 中myPackage
nuxt.config.js
...
build: {
transpile: [
'myPackage'
],
...
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
. myPackage
将Vue
作为依赖项,就像MySite1
、 MySite2
等一样。 如果项目在上链之前当前正在执行代码的项目,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.