本文介绍了Webpack无法导入从git安装的软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在git中包了一个包。做了我的改变。然后在我的终端中

  npm install --save git + https://github.com/hayk94/ddp.js.git 

然后我试着在我的代码中导入这个包作为这个

 从'ddp.js'导入DDP 

但是webpack给我这个错误$ / $>

  ./main.js中的错误
找不到模块:Error: '/ Users / hayksafaryan / projects / b2cEmbedLib'
@ ./main.js 23:11-28
@ multi(webpack)-dev-server / client无法解析'ddp.js' ?http:// localhost:8080 babel-polyfill ./main.js
webpack:无法编译。

但是如果我从npm安装包,webpack可以正常工作。
我在文档中导入软件包,但是对于安装git的软件包可能有其他方法吗?

,但该文件在存储库中不存在。图书馆在发布到npm之前构建图书馆是非常常见的,这样他们就可以使用更新的JavaScript功能,但仍然支持不支持它们的旧版本。这是通过,它会在发布包之前自动运行(当您运行 npm publish 时)。因此,构建的文件不会存储在存储库中,因为它主要会影响您的提交。有些人决定检查他们,以便他们可以直接从那里使用,这已经变得非常罕见,因为这些用例通常由像。



你可以从git仓库使用它。




  • 检入内建文件。
  • 安装后建立档案。手动或使用 postinstall 挂钩。 (不推荐

  • 将入口点改为 src / ddp.js 。如果您需要传输文件,这不是一个有效的选项,即使您理论上可以在您的webpack配置中将包列入白名单,所以它会被转发(假设您不包括 node_modules
  • 将包发布到名称空间下的npm( @ yourusername / ddp.js )并使用它。有关详细信息,请参见。


So I forked a package in the git. Made my changes. Then in my terminal

npm install --save git+https://github.com/hayk94/ddp.js.git

And then I try to import the package in my code as this

import DDP from 'ddp.js'

But webpack gives me this error

ERROR in ./main.js
Module not found: Error: Can't resolve 'ddp.js' in '/Users/hayksafaryan/projects/b2cEmbedLib'
 @ ./main.js 23:11-28
 @ multi (webpack)-dev-server/client?http://localhost:8080 babel-polyfill ./main.js
webpack: Failed to compile.

However webpack works fine if I install the package from npm.I import the package as in the docs, however maybe there is some other way for git installed packages?

解决方案

The entry point of the package is lib/ddp.js, but that file doesn't exist in the repository. It is very common that libraries build their libraries before publishing to npm, so that they can use newer JavaScript features but still provide support for older versions that don't support them. This is done with the prepublish hook, which is automatically run before the package is published (when you run npm publish). With that, the built files don't end up in the repository, as it would mainly clutter up your commits. Some people decide to check them in so they can use it directly from there, which has become quite rare because these use-cases are generally covered by services like Unpkg.

You have several possibilities to use it from a git repository.

  • Check in the built files.
  • Build the files after installing. Either manually or with a postinstall hook. (not recommended)
  • Change the entry point to src/ddp.js. If you need to transpile the files, this isn't a valid option, even though you could theoretically whitelist the package in your webpack config, so it gets transpiled (assuming you were excluding node_modules from being transpiled).
  • Publish the package to npm under your namespace (@yourusername/ddp.js) and use that. For details see Working with scoped packages.

这篇关于Webpack无法导入从git安装的软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:55