如何本地 npm 安装从 github 上面下载的开源包。

2020-08-04 18:10:55 +08:00
 ZhouWenhao529

现实需求: 执行‘npm install phantomjs-prebuilt’后,发现下载很慢,半个小时才走了 2%。 于是直接从 github 上下载了‘’phantomjs-prebuilt‘’的源代码,但是怎么把这个开源代码安装到项目的 node_modules 模块,却发现无从下手。跪求好心人指导一下,越详细越好,谢谢。 那个包 github 上面是一堆文件.

2345 次点击
所在节点    JavaScript
14 条回复
Rhianu
2020-08-04 18:13:46 +08:00
npm install username/reponame
wisunny
2020-08-04 18:15:32 +08:00
可以用镜像
Jirajine
2020-08-04 18:16:18 +08:00
npm install <folder>:

Install the package in the directory as a symlink in the current project. Its dependencies will be installed before it's linked. If <folder> sits inside the root of your project, its dependencies may be hoisted to the toplevel node_modules as they would for other types of dependencies.

npm install <tarball file>:

Install a package that is sitting on the filesystem. Note: if you just want to link a dev directory into your npm root, you can do this more easily by using npm link.

Tarball requirements:

The filename must use .tar, .tar.gz, or .tgz as the extension.
The package contents should reside in a subfolder inside the tarball (usually it is called package/). npm strips one directory layer when installing the package (an equivalent of tar x --strip-components=1 is run).
The package must contain a package.json file with name and version properties.

Example:

npm install ./package.tgz
yuang
2020-08-04 18:16:37 +08:00
package.json 文件中可以使用 file: 前缀的方式加载本地的包,第三方包就是这样开发测试的
ZhouWenhao529
2020-08-04 18:24:04 +08:00
@Jirajine 谢谢回复,我试一下
ZhouWenhao529
2020-08-04 18:24:23 +08:00
@yuang 谢谢回复,我试一下
ZhouWenhao529
2020-08-04 18:24:45 +08:00
@wisunny 这个要具体怎么操作呢?
ZhouWenhao529
2020-08-04 18:25:06 +08:00
@Rhianu 看不懂
wisunny
2020-08-04 19:30:25 +08:00
renmu123
2020-08-04 19:43:18 +08:00
package.json 也可以加载 github 的文件,推荐的应该是用阿里的镜像
ZhouWenhao529
2020-08-04 20:42:27 +08:00
@renmu123 谢谢回答,我试试
retanoj
2020-08-04 21:09:41 +08:00
npm install github:Medium/phantomjs-prebuilt

这样可以嘛?
不过好像这个库已经处于废弃状态了
ZhouWenhao529
2020-08-04 22:42:54 +08:00
@retanoj 是的,这是一个旧的代码
libook
2020-08-06 10:38:51 +08:00
直接复制或链接到你的 node_modules 里就可以了。

在你项目目录里
```
# 确保下载的包目录名字为 phantomjs-prebuilt
# 直接复制到 node_modules 里
cp -r 你下载包的位置 /phantomjs-prebuilt ./node_modules/

# 安装依赖
npm i
```

不过有可能其他包也下不下来,这种情况要么挂代理,要么用 cnpm (淘宝源)吧。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/695599

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX