简体   繁体   English

导入本地npm软件包时出错

[英]Error importing local npm package

We have several websites, each in its own project, and we are looking to migrate them all to using Vue.js. 我们有几个网站,每个网站都在自己的项目中,并且我们希望将它们全部迁移到使用Vue.js。 Each website has its own directory with .vue files that are then bundled using Webpack. 每个网站都有其自己的目录,其中包含.vue文件,然后使用Webpack将其捆绑在一起。 We have a Webpack config in place that converts the .vue files, bundles, lints and pipes it all through babel and it works fine. 我们有一个Webpack配置,可以转换.vue文件,捆绑,棉绒并通过babel将其通过管道传输,并且运行良好。

However, now that we have been moving things over for several weeks we have noticed that there are several components and core javascript files that are very similar and ideally we want to pull these out into a shared library of vue components and functions. 但是,既然我们已经进行了数周的工作,我们已经注意到有一些组件和核心javascript文件非常相似,并且理想情况下,我们希望将它们引入vue组件和功能的共享库中。

We have extracted several .vue into a folder alongside the websites, and put them together as a basic npm module with its own package.json , and include them using an npm file include, so in the package.json it just looks like: "vue-shared": "file:../CommonJavascript/Vue" . 我们提取了几个.vue进入网站旁的文件夹,并把它们放在一起,与自己的一个基本的NPM模块package.json ,并使用NPM文件包括包括,所以在package.json它只是看起来像: "vue-shared": "file:../CommonJavascript/Vue" Now when we try to use Webpack to build the bundle, we get the error: 现在,当我们尝试使用Webpack生成捆绑包时,我们得到了以下错误:

ERROR in ../CommonJavascript/Vue/index.js Module build failed (from ./node_modules/eslint-loader/index.js): Error: Failed to load plugin react: Cannot find module 'eslint-plugin-react' ../CommonJavascript/Vue/index.js中的错误模块构建失败(来自./node_modules/eslint-loader/index.js):错误:无法加载插件反应:找不到模块'eslint-plugin-react'

I'm not sure where this error is coming from, because we aren't using react anywhere, and it seemed happy enough to build fine before we moved the files out. 我不确定此错误来自何处,因为我们没有在任何地方使用react,并且在将文件移出之前看起来很高兴可以很好地构建。 At the moment the only dependency in the shared module is moment, and it only contains 4 .vue , and a basic wrapper to bundle them up: 目前,共享模块中唯一的依赖项是moment,它仅包含4 .vue以及将它们捆绑在一起的基本包装器:

import button from 'Button.vue'
import loading from 'Loading.vue'
import modal from 'Modal.vue'
import progressBar from 'ProgressBar.vue'

export default {
  button,
  loading,
  modal,
  progressBar,
}

But, I was curious so I decided to add the package (even though we don't need it) to see if it would fix the issue, but I then get a new error: 但是,我很好奇,所以我决定添加软件包(即使我们不需要它)以查看它是否可以解决问题,但是随后出现一个新错误:

ERROR in ../CommonJavascript/Vue/index.js Module build failed (from ./node_modules/babel-loader/lib/index.js): ReferenceError: Unknown plugin "transform-runtime" specified in "base" at 0, attempted to resolve relative to "C:\\Projects\\Tmo\\Code\\CommonJavascript\\Vue" ../CommonJavascript/Vue/index.js中的错误模块构建失败(来自./node_modules/babel-loader/lib/index.js):ReferenceError:尝试在“基本”中将0指定为未知插件“ transform-runtime”相对于“ C:\\ Projects \\ Tmo \\ Code \\ CommonJavascript \\ Vue”进行解析

Now, that one makes a little more sense, we do use the babel runtime transform on the main project, but it isn't required by anything in the shared project and even if it was , surely the fact it is included in the main project means it should still build. 现在,一个让多一点意义上,我们确实使用主体工程通天运行时变换,但它不是由任何在共享项目中必需的,即使 ,肯定是事实,它被包含在主体工程表示它仍应构建。

Partly, it seems perhaps I'm just not understanding the way npm resolves dependencies. 在某种程度上,似乎我只是不了解npm解决依赖关系的方式。 It seems to be trying to now resolve some dependencies by looking in the shared files project and I dont know why. 它似乎正在尝试通过查看共享文件项目来解决一些依赖性,但我不知道为什么。 Also I have no idea where this strange dependency on eslint-plugin-react has come from. 我也不知道这种对eslint-plugin-react的奇怪依赖来自何处。

So I guess this is a multi-part question. 所以我想这是一个多部分的问题。 What is up with the way npm is trying to resolve the dependencies? npm尝试解决依赖关系的方式是什么? Am I doing things right by moving the .vue files into a separate project, wrapping it up as a module and requiring it in the main project? 通过将.vue文件移动到一个单独的项目中,将其包装为模块并在主项目中需要它,我是否做对了? and if not, what is the best way to have shared dependencies like this? 如果没有,拥有这样的共享依赖关系的最佳方法是什么?

This was caused by a mixture of two separate issues: 这是由两个独立的问题混合引起的:

  • The import statements didn't reference the file properly, the correct syntax is: import button from './Button.vue' (note the change to file path) 导入语句未正确引用文件,正确的语法为: import button from './Button.vue' (请注意对文件路径的更改)
  • When you add a local package to npm via a path, it creates a symlink to the folder rather than copying the files over (this has been the behaviour since npm v5+). 当您通过路径将本地软件包添加到npm时,它会创建一个指向文件夹的符号链接,而不是复制文件(这是npm v5 +以来的行为)。 This then changes the way webpack tries to resolve dependencies since it then looks up from the location of the shared files to try and resolve dependencies including thing like eslint and babel. 然后,这将更改webpack尝试解析依赖项的方式,因为它随后从共享文件的位置查找以尝试解决包含eslint和babel之类的依赖项。
  • The eslint-plugin-react dependency was because in visual studio code I had installed the eslint plugin, which it seems had created a .eslintrc file which reference the react plugin in my user folder (c:\\users\\<username>). eslint-plugin-react依赖关系是因为我在Visual Studio代码中安装了eslint插件,该插件似乎创建了一个.eslintrc文件,该文件引用了我的用户文件夹(c:\\ users \\ <username>)中的react插件。 Eslint will then use this as the default if it can't find a config file (which it couldn't because it was looking above the shared files because of the pathing issues described above) 如果找不到配置文件,则Eslint将其用作默认值(由于上述路径问题,它无法在共享文件上方查找,所以无法找到该配置文件)

We have decided we will be using a git submodule for these files going forward 我们已经决定以后将为这些文件使用git子模块

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

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