Webpack 无法导入从 git 安装的包 [英] Webpack can't import package installed from git

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

问题描述

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

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'

但是 webpack 给了我这个错误

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.

但是,如果我从 npm 安装包,webpack 可以正常工作.我像在文档中一样导入包,但是对于 git 安装的包,也许还有其他方法?

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?

推荐答案

包是 lib/ddp.js,但该文件在存储库中不存在.库在发布到 npm 之前构建它们的库是很常见的,这样它们就可以使用较新的 JavaScript 功能,但仍然为不支持它们的旧版本提供支持.这是通过 prepublish 挂钩,在发布包之前自动运行(当你运行 npm publish 时).这样一来,构建的文件就不会出现在存储库中,因为它主要会使您的提交变得混乱.有些人决定签入它们,以便他们可以直接从那里使用它,这已经变得非常罕见,因为这些用例通常由诸如 解包.

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.

您有多种可能从 git 存储库中使用它.

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

  • 签入构建的文件.
  • 安装后构建文件.手动或使用 postinstall 钩子.(不推荐)
  • 将入口点更改为 src/ddp.js.如果您需要转译文件,这不是一个有效的选项,即使理论上您可以在 webpack 配置中将该包列入白名单,因此它会被转译(假设您将 node_modules 排除在被转译之外).
  • 将包发布到您的命名空间 (@yourusername/ddp.js) 下的 npm 并使用它.有关详细信息,请参阅使用范围包.
  • 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 安装的包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆