使用 npm 安装本地模块?

发布于 2024-12-14 13:33:28 字数 70 浏览 1 评论 0原文

我有一个下载的模块存储库,我想将其安装在本地,而不是全局安装在另一个目录中?

有什么简单的方法可以做到这一点?

I have a downloaded module repo, I want to install it locally, not globally in another directory?

What is an easy way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

書生途 2024-12-21 13:33:28

您只需向 npm install参数>,参数应指向本地文件夹而不是包名称:

npm install /path

you just provide one <folder> argument to npm install, argument should point toward the local folder instead of the package name:

npm install /path
倾其所爱 2024-12-21 13:33:28

来自 npm-link 文档

在本地模块目录中:

$ cd ./package-dir
$ npm link

在要使用的项目目录中模块:

$ cd ./project-dir
$ npm link package-name

或者一次性使用相对路径:

$ cd ./project-dir
$ npm link ../package-dir

这相当于在后台使用上面的两个命令。

From the npm-link documentation:

In the local module directory:

$ cd ./package-dir
$ npm link

In the directory of the project to use the module:

$ cd ./project-dir
$ npm link package-name

Or in one go using relative paths:

$ cd ./project-dir
$ npm link ../package-dir

This is equivalent to using two commands above under the hood.

抱猫软卧 2024-12-21 13:33:28

由于提问和回答是同一个人,我将添加一个 npm 链接 作为替代方案。

来自文档:

这对于安装您自己的东西很方便,这样您就可以对其进行处理并迭代测试它,而无需不断重建。

cd ~/projects/node-bloggy  # go into the dir of your main project
npm link ../node-redis     # link the dir of your dependency

[编辑] 从 NPM 2.0 开始,您可以在 package.json 中声明本地依赖项

"dependencies": {
    "bar": "file:../foo/bar"
  }

Since asked and answered by the same person, I'll add a npm link as an alternative.

from docs:

This is handy for installing your own stuff, so that you can work on it and test it iteratively without having to continually rebuild.

cd ~/projects/node-bloggy  # go into the dir of your main project
npm link ../node-redis     # link the dir of your dependency

[Edit] As of NPM 2.0, you can declare local dependencies in package.json

"dependencies": {
    "bar": "file:../foo/bar"
  }
北城半夏 2024-12-21 13:33:28

npm pack + package.json

这对我有用:

第1步:在模块项目中,执行npm pack >:

这将构建一个 -.tar.gz 文件。

第 2 步:将文件移动到 consumer project

理想情况下,您可以将所有此类文件放入 consumer-project 根目录中的 tmp 文件夹中:

STEP 3:在 package.json 中引用它:

"dependencies": {
  "my-package": "file:/./tmp/my-package-1.3.3.tar.gz"
}

第 4 步:安装 软件包:

npm installnpm iyarn

现在,你的该包将在您的消费者项目的 node_modules 文件夹中提供。

祝你好运...

npm pack + package.json

This is what worked for me:

STEP 1: In module project, execute npm pack:

This will build a <package-name>-<version>.tar.gz file.

STEP 2: Move the file to the consumer project

Ideally you can put all such files in a tmp folder in your consumer-project root:

STEP 3: Refer it in your package.json:

"dependencies": {
  "my-package": "file:/./tmp/my-package-1.3.3.tar.gz"
}

STEP 4: Install the packages:

npm install or npm i or yarn

Now, your package would be available in your consumer-project's node_modules folder.

Good Luck...

琉璃梦幻 2024-12-21 13:33:28

如果本地模块具有您只想在项目范围内安装的对等依赖项,那么这两种方法(npm linkpackage.json 文件依赖项)都不起作用。

例如:

/local/mymodule/package.json

"name": "mymodule",
"peerDependencies":
{
  "foo": "^2.5"
}

/dev/myproject/package.json

"dependencies":
{
  "mymodule": "file:/local/mymodule",
  "foo": "^2.5"
}

在这种情况下,npm 设置 myprojectnode_modules/ 像这样:

/dev/myproject/node_modules/
 ↳ foo/
 ↳ mymodule -> /local/mymodule

当节点加载 mymodule 并执行 require('foo') 时,节点会解析 mymodule > 符号链接,然后只查找/local/mymodule/node_modules/ (及其祖先)用于 foo,但它找不到。相反,我们希望节点在 /local/myproject/node_modules/ 中查找,因为这是我们运行项目的位置,也是安装 foo 的位置。

因此,我们要么需要一种方法来告诉节点在查找 foo解析此符号链接,或者我们需要一种方法来告诉 npm 安装副本<当在 package.json 中使用文件依赖语法时,mymodule 的 /em>。不幸的是,我还没有找到一种方法:(

Neither of these approaches (npm link or package.json file dependency) work if the local module has peer dependencies that you only want to install in your project's scope.

For example:

/local/mymodule/package.json

"name": "mymodule",
"peerDependencies":
{
  "foo": "^2.5"
}

/dev/myproject/package.json

"dependencies":
{
  "mymodule": "file:/local/mymodule",
  "foo": "^2.5"
}

In this scenario, npm sets up myproject's node_modules/ like this:

/dev/myproject/node_modules/
 ↳ foo/
 ↳ mymodule -> /local/mymodule

When node loads mymodule and it does require('foo'), node resolves the mymodule symlink, and then only looks in /local/mymodule/node_modules/ (and its ancestors) for foo, which it doen't find. Instead, we want node to look in /local/myproject/node_modules/, since that's where were running our project from, and where foo is installed.

So, we either need a way to tell node to not resolve this symlink when looking for foo, or we need a way to tell npm to install a copy of mymodule when the file dependency syntax is used in package.json. I haven't found a way to do either, unfortunately :(

陌路终见情 2024-12-21 13:33:28

缺少主要财产?

正如之前的人回答的那样,npm i --save ../location-of-your-packages-root-directory
然而,../location-of-your-packages-root-directory 必须具备两件事才能正常工作。

  1. 该目录中的

    package.json 指向

  2. package.json 中的

    main 属性,必须设置并工作 "main ": "src/index.js", 如果 ../location-of-your-packages-root-directory 的入口文件是../location-of-your-packages-root-directory/src/index.js

Missing the main property?

As previous people have answered npm i --save ../location-of-your-packages-root-directory.
The ../location-of-your-packages-root-directory however must have two things in order for it to work.

  1. package.json in that directory pointed towards

  2. main property in the package.json must be set and working i.g. "main": "src/index.js", if the entry file for ../location-of-your-packages-root-directory is ../location-of-your-packages-root-directory/src/index.js

七堇年 2024-12-21 13:33:28

所以我对到目前为止提到的所有解决方案都遇到了很多问题...

我有一个我想始终引用的本地包(而不是 npm 链接),因为它不会在该项目之外使用(目前)并且目前还不会上传到 npm 存储库以供广泛使用。

我还需要它在 Windows 和 Unix 上工作,所以符号链接并不理想。

指向(npm 包)的 tar.gz 结果适用于依赖的 npm 包文件夹,但是如果您想更新包,这会导致 npm 缓存出现问题。当你更新它时,它并不总是从引用的 npm 包中提取新的,即使你删除了 node_modules 并为你的主项目重新安装了 npm-install 。

所以..这对我来说很有效!

主项目的 Package.json 文件片段:

  "name": "main-project-name",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    ...
    "preinstall": "cd ../some-npm-package-angular && npm install && npm run build"
  },
  "private": true,
  "dependencies": {
    ...
    "@com/some-npm-package-angular": "file:../some-npm-package-angular/dist",
    ...
  }

这实现了 3 件事:

  • 避免常见错误(至少对于 Angular npm 项目)“index.ts 不是编译的一部分”。 - 因为它指向构建的(dist)文件夹。
  • 添加预安装步骤来构建引用的 npm 客户端包,以确保构建我们的依赖包的 dist 文件夹。
  • 避免本地引用 tar.gz 文件可能会被 npm 缓存,并且在没有大量清理/故障排除/重新构建/重新安装的情况下不会在主项目中更新的问题。

我希望这是清楚的,并能帮助别人。

tar.gz 方法也有点工作

。npm install(文件路径)也有点工作。

这一切都是基于 openapi 规范生成的客户端,我们希望将其保留在单独的位置(而不是对单个文件使用 copy-pasta)

======
更新:
======

使用上述解决方案进行常规开发流程还会出现其他错误,因为 npm 的本地文件版本控制方案非常糟糕。如果您的依赖包经常更改,整个方案就会被破坏,因为 npm 会缓存项目的最后一个版本,然后当 SHA 哈希值与 package-lock.json 文件中保存的内容不再匹配时就会崩溃,以及其他问题。

因此,我建议使用 *.tgz 方法,并为每次更改进行版本更新。这是通过做三件事来实现的。

首先:

对于您的依赖包,请使用 npm 库“ng-packagr”。这会自动添加到由 OpenAPI 3.0 的 Angular-TypeScript 代码生成器创建的自动生成的客户端包中。

因此,我引用的项目在 package.json 中有一个“脚本”部分,如下所示:

  "scripts": {
    "build": "ng-packagr -p ng-package.json",
    "package": "npm install && npm run build && cd dist && npm pack"
  },

引用其他项目的项目添加了一个预安装步骤,以确保依赖项目是最新的并重新构建在构建自身之前:

  "scripts": {
    "preinstall": "npm run clean && cd ../some-npm-package-angular && npm run package"
  },

第二

从主项目引用构建的 tgz npm 包!

  "dependencies": {
    "@com/some-npm-package-angular": "file:../some-npm-package-angular/dist/some-npm-package-angular-<packageVersion>.tgz",
    ...
  }

第三

每次更新依赖包时都更新依赖包的版本。您还必须更新主项目中的版本。

如果您不这样做,NPM 将阻塞并使用缓存版本,并在 SHA 哈希不匹配时爆炸。 NPM 根据文件名更改来版本基于文件的包。它不会检查包本身是否有 package.json 中的更新版本,NPM 团队表示他们不会修复此问题,但人们不断提出这个问题:https://github.com/microsoft/WSL/issues/348

现在,只需更新:

"version": "1.0.0-build5",

在依赖包的 package.json 文件中,然后更新您在主项目中对它的引用以供参考新的文件名,例如:

"dependencies": {
       "@com/some-npm-package-angular": "file:../some-npm-package-angular/dist/some-npm-package-angular-1.0.0-build5.tgz",
        ...
}

你已经习惯了。只需更新两个 package.json 文件 - 版本,然后引用新文件名。

希望能帮助某人...

So I had a lot of problems with all of the solutions mentioned so far...

I have a local package that I want to always reference (rather than npm link) because it won't be used outside of this project (for now) and also won't be uploaded to an npm repository for wide use as of yet.

I also need it to work on Windows AND Unix, so sym-links aren't ideal.

Pointing to the tar.gz result of (npm package) works for the dependent npm package folder, however this causes issues with the npm cache if you want to update the package. It doesn't always pull in the new one from the referenced npm package when you update it, even if you blow away node_modules and re-do your npm-install for your main project.

so.. This is what worked well for me!

Main Project's Package.json File Snippet:

  "name": "main-project-name",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    ...
    "preinstall": "cd ../some-npm-package-angular && npm install && npm run build"
  },
  "private": true,
  "dependencies": {
    ...
    "@com/some-npm-package-angular": "file:../some-npm-package-angular/dist",
    ...
  }

This achieves 3 things:

  • Avoids the common error (at least with angular npm projects) "index.ts is not part of the compilation." - as it points to the built (dist) folder.
  • Adds a preinstall step to build the referenced npm client package to make sure the dist folder of our dependent package is built.
  • Avoids issues where referencing a tar.gz file locally may be cached by npm and not updated in the main project without lots of cleaning/troubleshooting/re-building/re-installing.

I hope this is clear, and helps someone out.

The tar.gz approach also sort of works..

npm install (file path) also sort of works.

This was all based off of a generated client from an openapi spec that we wanted to keep in a separate location (rather than using copy-pasta for individual files)

======
UPDATE:
======

There are additional errors with a regular development flow with the above solution, as npm's versioning scheme with local files is absolutely terrible. If your dependent package changes frequently, this whole scheme breaks because npm will cache your last version of the project and then blow up when the SHA hash doesn't match anymore with what was saved in your package-lock.json file, among other issues.

As a result, I recommend using the *.tgz approach with a version update for each change. This works by doing three things.

First:

For your dependent package, use the npm library "ng-packagr". This is automatically added to auto-generated client packages created by the angular-typescript code generator for OpenAPI 3.0.

As a result the project that I'm referencing has a "scripts" section within package.json that looks like this:

  "scripts": {
    "build": "ng-packagr -p ng-package.json",
    "package": "npm install && npm run build && cd dist && npm pack"
  },

And the project referencing this other project adds a pre-install step to make sure the dependent project is up to date and rebuilt before building itself:

  "scripts": {
    "preinstall": "npm run clean && cd ../some-npm-package-angular && npm run package"
  },

Second

Reference the built tgz npm package from your main project!

  "dependencies": {
    "@com/some-npm-package-angular": "file:../some-npm-package-angular/dist/some-npm-package-angular-<packageVersion>.tgz",
    ...
  }

Third

Update the dependent package's version EVERY TIME you update the dependent package. You'll also have to update the version in the main project.

If you do not do this, NPM will choke and use a cached version and explode when the SHA hash doesn't match. NPM versions file-based packages based on the filename changing. It won't check the package itself for an updated version in package.json, and the NPM team stated that they will not fix this, but people keep raising the issue: https://github.com/microsoft/WSL/issues/348

for now, just update the:

"version": "1.0.0-build5",

In the dependent package's package.json file, then update your reference to it in the main project to reference the new filename, ex:

"dependencies": {
       "@com/some-npm-package-angular": "file:../some-npm-package-angular/dist/some-npm-package-angular-1.0.0-build5.tgz",
        ...
}

You get used to it. Just update the two package.json files - version then the ref to the new filename.

Hope that helps someone...

我三岁 2024-12-21 13:33:28

在为 CKEditor5 安装自定义构建包时,我遇到了与上述不同的解决方案。

因此,我将包上传到应用程序根目录,然后:

npm add file:./ckeditor5

在我的 package.json 中,包被列为文件:

"ckeditor5-custom-build": "file:ckeditor5",

我认为这个答案可能与如何添加本地包的主题相关。

I came across different solution than above while installing custom build package for CKEditor5.

So I uploaded package to app root directory, than:

npm add file:./ckeditor5

In my package.json package is listed as a file:

"ckeditor5-custom-build": "file:ckeditor5",

I think this answer could be relevant to the topic on how to add local package.

可爱暴击 2024-12-21 13:33:28

正如 @fancy 接受的答案中所解释的,您可以使用此命令:

npm install ...<path_to_your_local_package>

添加本地软件包。

在项目的 package.json 中,它将创建一个条目,如下所示:

"dependencies": {
    ...
    "your_package_name": "file:...<path_to_your_local_package>"
    ...
}

如果您包含的包位于项目根目录中,那么它将安装包含的本地包的所有依赖项。否则,即如果它位于项目根目录之外,它只会创建一个符号链接(如 @frank-tan 指出的那样),在这种情况下,如果由于某种原因您删除了 node_modules 目录您的项目或者需要重新安装,您必须运行:

npm install --install-links

命令行选项install-links确保自动安装本地软件包的所有依赖项。例如,如果您正在使用 Jenkins 并且需要部署具有许多自定义开发的嵌套依赖项的大型项目,这将派上用场。

请参阅官方 npm-install 文档了解更多详细信息
https://docs.npmjs.com/cli/v9/commands/npm-安装

As explained in the accepted answer by @fancy, you can use this command:

npm install ...<path_to_your_local_package>

to add your local packages.

In the package.json of your project it will create an entry like:

"dependencies": {
    ...
    "your_package_name": "file:...<path_to_your_local_package>"
    ...
}

If the package you're including is within the project root, then it will do an installation of all the dependencies of your included local package. Otherwise, i.e. if it's outside your project root, it will simply create a symbolic link (as @frank-tan pointed out), in which case, if for some reason you've deleted the node_modules directory in your project or you need to do a fresh reinstall you must run:

npm install --install-links

The command line option install-links ensures that all dependencies of the local packages get installed automatically. This will come in handy if, for example, you're using Jenkins and need to deploy a large project with many custom-developed nested dependencies.

See official npm-install documentation for more detail:
https://docs.npmjs.com/cli/v9/commands/npm-install

还不是爱你 2024-12-21 13:33:28

对于安装本地模块/包,尚未在 npm 上安装,或者您正在开发 npm 包并希望在发布之前在本地测试它。您可以尝试此操作 -

npm i yalc -g

转到模块/包文件夹,然后 -

yalc publish

您的包已准备好使用,现在转到您想要安装的项目 -

yalc add <Your package name>

包将安装到您的项目中。如果你想删除它 -

yalc remove <Your package name>

For installing local module / package, that not yet on npm or you are developing an npm package and want to test it locally before publishing it. You can try this -

npm i yalc -g

Go to the module/package folder then -

yalc publish

Your packakge is ready to use, now go the project you want to install it -

yalc add <Your package name>

Package will be installed to you project. If you want to remove it -

yalc remove <Your package name>
无人问我粥可暖 2024-12-21 13:33:28

保持简单!

您可以使用 lnk-cli 创建目录链接。

这是我使用 TypeScript 的设置:

  • common(包含我想要共享的所有 .ts 文件)
  • server(这是 BullBoard 服务器)
  • 函数(这是一个 Firebase 项目)

第 1 步:安装 lnk-cli

  • 全局安装,请参阅 这里

步骤 2:公用文件夹

在此处输入图像描述

仅包含您想要的代码文件(无 package.jsontsconfig.json 等)

第 3 步:Package.json

在此处输入图像描述

查看突出显示的文本。只需添加“lnk”命令即可在观看 TypeScript 文件之前创建目录链接。

步骤 4:.gitignore

在此处输入图像描述

common 文件夹包含主要代码,因此您可以忽略这些链接的文件夹。三个位置(common、functions/common 或 server/common)之一的任何更改都将自动反映在所有链接的位置中。

完毕!

输入图片此处描述

现在您可以像这样导入常用文件了。 (无需额外调整tsconfig.json)

Keep It Simple!

You can use lnk-cli to create a directory link.

Here’s my setup using TypeScript:

  • common (Contains all .ts files I want to share)
  • server (This is a BullBoard server)
  • functions (This is a Firebase project)

Step 1: Install lnk-cli

  • Install globally, see here.

Step 2: Common Folder

enter image description here

Only include the code files you want (no package.json, tsconfig.json, etc.)

Step 3: Package.json

enter image description here

See the highlighted text. Just add the "lnk" command to create the directory link before watching the TypeScript files.

Step 4: .gitignore

enter image description here

The common folder has the main code, so you can ignore these linked folders. Any changes in one of the three locations (common, functions/common, or server/common) will automatically reflect in all linked locations.

Done!

enter image description here

Now you can import the common files like this. (No additional need to adjust tsconfig.json)

冰雪之触 2024-12-21 13:33:28

对于更新版本的 npm(我在 macOS Big Sur 下使用 8.1.3),命令序列甚至更简单...

cd /path-where-your-local-project-is/
npm init

这将要求您提供一些与项目相关的数据并正确初始化您的项目。 json 文件。

完成后,您可以安装其他模块:

cd /path-where-your-local-project-is/
npm install --save-dev some-npm-module .

这就是您所需要的!

注意:我相信如果您在项目目录中,则不需要尾随点,但我也认为添加它并没有什么坏处:-)

(我想知道为什么官方文档还是不解释这个...)

For more recent versions of npm (I'm using 8.1.3 under macOS Big Sur), the sequence of commands is even easier...

cd /path-where-your-local-project-is/
npm init

This will ask you for some data related to your project and properly initialises your project.json file.

Once that is done, you can install additional modules with:

cd /path-where-your-local-project-is/
npm install --save-dev some-npm-module .

That's all you need!

Note: I believe that the trailing dot is not necessary if you're inside the project directory, but I also think that it doesn't hurt to add it :-)

(I wonder why the official docs still don't explain this...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文