简体   繁体   English

package.json 中的本地依赖

[英]Local dependency in package.json

I want to do something like this, so npm install also installs the package.json of ../somelocallib or more importantly its dependencies.我想做这样的事情,所以npm install还会安装package.json../somelocallib或更重要的是它的依赖项。

"dependencies": {
    "express": "*",
    "../somelocallib": "*"
}

npm >= 2.0.0 npm >= 2.0.0

This feature was implemented in the version 2.0.0 of npm.这个特性是在 npm 2.0.0 版本中实现的。 Example:例子:

{
  "name": "baz",
  "dependencies": {
    "bar": "file:../foo/bar"
  }
}

Any of the following paths are also valid:以下任何路径也有效:

../foo/bar
~/foo/bar
./foo/bar
/foo/bar

The local package will be copied to the prefix ( ./node-modules ).本地包将被复制前缀./node-modules )。

npm < 2.0.0 npm < 2.0.0

Put somelocallib as dependency in your package.json as normal: somelocallib一样将somelocallib作为依赖项放在package.json中:

"dependencies": {
  "somelocallib": "0.0.x"
}

Then run npm link ../somelocallib and npm will install the version you're working on as a symlink .然后运行npm link ../somelocallib ,npm 将安装您正在使用的版本作为 symlink

app@0.0.1 /private/tmp/app
└── somelocallib@0.0.1 -> /private/tmp/somelocallib

Reference: link(1)参考:链接(1)

It is now possible to specify local Node module installation paths in your package.json directly.现在可以直接在package.json指定本地 Node 模块安装路径。 From the docs:从文档:

Local Paths本地路径

As of version 2.0.0 you can provide a path to a local directory that contains a package.从版本 2.0.0 开始,您可以提供包含包的本地目录的路径。 Local paths can be saved using npm install -S or npm install --save , using any of these forms:可以使用npm install -Snpm install --save保存本地路径,使用以下任何形式:

 ../foo/bar ~/foo/bar ./foo/bar /foo/bar

in which case they will be normalized to a relative path and added to your package.json .在这种情况下,它们将被规范化为相对路径并添加到您的package.json For example:例如:

 { "name": "baz", "dependencies": { "bar": "file:../foo/bar" } }

This feature is helpful for local offline development and creating tests that require npm installing where you don't want to hit an external server, but should not be used when publishing packages to the public registry.此功能有助于本地离线开发和创建需要在不想访问外部服务器的地方安装 npm 的测试,但不应在将包发布到公共注册表时使用。

This works for me.这对我有用。

Place the following in your package.json file将以下内容放入您的 package.json 文件中

"scripts": {
    "preinstall": "npm install ../my-own-module/"
}

If you want to further automate this, because you are checking your module into version control, and don't want to rely upon devs remembering to npm link, you can add this to your package.json "scripts" section:如果您想进一步自动化此操作,因为您正在将模块检查到版本控制中,并且不想依赖于记住 npm 链接的开发人员,您可以将其添加到您的 package.json “脚本”部分:

"scripts": {
    "postinstall": "npm link ../somelocallib",
    "postupdate": "npm link ../somelocallib"
  }

This feels beyond hacky, but it seems to "work".这感觉超出了hacky,但它似乎“有效”。 Got the tip from this npm issue: https://github.com/npm/npm/issues/1558#issuecomment-12444454从这个 npm 问题中得到提示: https : //github.com/npm/npm/issues/1558#issuecomment-12444454

This is how you will add local dependencies:这是添加本地依赖项的方式:

npm install file:src/assets/js/FILE_NAME

Add it to package.json from NPM:从 NPM 将其添加到 package.json 中:

npm install --save file:src/assets/js/FILE_NAME

Directly add to package.json like this:像这样直接添加到 package.json 中:

....
  "angular2-autosize": "1.0.1",
  "angular2-text-mask": "8.0.2", 
  "animate.css": "3.5.2",
  "LIBRARY_NAME": "file:src/assets/js/FILE_NAME"
....

Master project主项目

Here is the package.json you will use for the master project:这是您将用于主项目的 package.json:

"dependencies": {
    "express": "*",
    "somelocallib": "file:./somelocallib"
}

There, ./somelocallib is the reference to the library folder as relative to the master project package.json .在那里, ./somelocallib相对于主项目 package.json对库文件夹的引用。

Reference: https://docs.npmjs.com/cli/v7/configuring-npm/package-json#local-paths参考: https : //docs.npmjs.com/cli/v7/configuring-npm/package-json#local-paths


Sub project子项目

Handle your library dependencies.处理您的库依赖项。

In addition to running npm install , you will need to run (cd node_modules/somelocallib && npm install) .除了运行npm install ,您还需要运行(cd node_modules/somelocallib && npm install)

This is a known bug with NPM.这是 NPM 的一个已知错误。

Reference: https://github.com/npm/npm/issues/1341 (seeking a more up-to-date reference)参考: https : //github.com/npm/npm/issues/1341 (寻求更新的参考)


Notes for Docker Docker 的注意事项

Check in your master package.lock and your somelocallib/package.lock into your source code manager.将您的主package.locksomelocallib/package.lock入源代码管理器。

Then in your Dockerfile use:然后在您的 Dockerfile 中使用:

FROM node:10
WORKDIR /app
# ...
COPY ./package.json ./package-lock.json ./
COPY somelocallib somelocallib
RUN npm ci
RUN (cd node_modules/zkp-utils/ && npm ci)
# ...

I use parenthesis in my (cd A && B) constructs to make the operation idempotent.我在(cd A && B)构造中使用括号来使操作具有幂等性。

Two steps for a complete local development:完整本地开发的两个步骤:

  1. Provide the path to the local directory that contains the package. 提供包含包的本地目录的路径。
 { "name": "baz", "dependencies": { "bar": "file:../foo/bar" } }
  1. Symlink the package folder符号链接包文件夹

    cd ~/projects/node-redis # go into the package directory npm link # creates global link cd ~/projects/node-bloggy # go into some other package directory. npm link redis # link-install the package

Here in 2020, working on a Windows 10, I tried with在 2020 年,我在 Windows 10 上工作,我尝试使用

"dependencies": {
    "some-local-lib": "file:../../folderY/some-local-lib" 
    ...
}

Then doing a npm install.然后进行npm安装。 The result is that a shortcut to the folder is created in node-modules .结果是在node-modules创建了文件夹的快捷方式。 This doesn't work.这不起作用。 You need a hard link - which windows support, but you have to do something extra in windows to create a hard symlink.您需要一个硬链接 - Windows 支持,但您必须在 Windows 中做一些额外的事情来创建一个硬符号链接。

Since I don't really want a hard link, I tried using an url instead:由于我真的不想要硬链接,因此我尝试使用 url:

"dependencies": {
    "some-local-lib": "file:///D:\\folderX\\folderY\\some-local-lib.tar" 
     ....
}

And this works nicely.这很好用。
The tar (you have to tar the stuff in the library's build / dist folder) gets extracted to a real folder in node-modules, and you can import like everything else. tar(您必须对库的 build / dist 文件夹中的内容进行 tar)提取到节点模块中的真实文件夹中,您可以像其他所有内容一样导入。
Obviously the tar part is a bit annoying, but since 'some-local-lib' is a library (which has to be build anyway), I prefer this solution to creating a hard link or installing a local npm.显然 tar 部分有点烦人,但由于 'some-local-lib' 是一个库(无论如何都必须构建),我更喜欢这个解决方案来创建硬链接或安装本地 npm。

I know that npm install ../somelocallib works.我知道npm install ../somelocallib可以工作。

However, I don't know whether or not the syntax you show in the question will work from package.json ...但是,我不知道您在问题中显示的语法是否适用于package.json ...

Unfortunately, doc seems to only mention URL as a dependency.不幸的是, doc似乎只提到 URL 作为依赖项。

Try file:///.../...tar.gz , pointing to a zipped local lib... and tell us if it works.尝试file:///.../...tar.gz ,指向一个压缩的本地库...并告诉我们它是否有效。

Curious.....at least on Windows (my npm is 3.something) I needed to do:好奇.....至少在 Windows 上(我的 npm 是 3.something)我需要做:

"dependencies": {
 "body-parser": "^1.17.1",
 "module1": "../module1",
 "module2": "../module2",

When I did an npm install ../module1 --save it resulted in absolute paths and not relative per the documentation.当我执行npm install ../module1 --save它会根据文档生成绝对路径而不是相对路径。

I messed around a little more and determined that ../xxx was sufficient.我又搞砸了一点,并确定../xxx就足够了。

Specifically, I have the local node modules checked out to say d:\\build\\module1, d:\\build\\module2 and my node project (application) in d:\\build\\nodeApp.具体来说,我已检出本地节点模块以表示 d:\\build\\module1、d:\\build\\module2 和 d:\\build\\nodeApp 中的我的节点项目(应用程序)。

To 'install', I:要“安装”,我:

d:\build\module1> rmdir "./node_modules" /q /s && npm install
d:\build\module2> rmdir "./node_modules" /q /s && npm install
d:\build\nodeApp> rmdir "./node_modules" /q /s && npm install

module1's package.json has a dependency of "module2": "../module2"; module1 的 package.json 依赖于 "module2": "../module2"; module2 has no local dependency; module2 没有本地依赖; nodeApp has dependencies "module1": "../module1" and "module2": "../module2". nodeApp 具有依赖项“module1”:“../module1”和“module2”:“../module2”。

Not sure if this only works for me since all 3 folders (module1, module2 and nodeApp) sit on that same level.......不确定这是否仅适用于我,因为所有 3 个文件夹(module1、module2 和 nodeApp)都位于同一级别......

This worked for me: first, make sure the npm directories have the right user这对我有用:首先,确保 npm 目录具有正确的用户

sudo chown -R myuser ~/.npm
sudo chown -R myuser /usr/local/lib/node_modules

Then your in your package.json link the directory然后你在你的 package.json 链接目录

"scripts": {
 "preinstall": "npm ln mylib ../../path/to/mylib"
}, 
"dependencies": {
  "mylib" : "*"
}

实际上,从 npm 2.0 开始,现在支持本地路径(请参阅 此处)。

There is great yalc that helps to manage local packages.有很棒的yalc可以帮助管理本地包。 It helped me with local lib that I later deploy.它帮助我使用了我后来部署的本地库。 Just pack project with .yalc directory (with or without /node_modules).只需使用 .yalc 目录打包项目(带或不带 /node_modules)。 So just do:所以只需这样做:

npm install -g yalc  

in directory lib/$ yalc publish 

in project:在项目中:

project/$ yalc add lib

project/$ npm install 

that's it.而已。

When You want to update stuff:当您想更新内容时:

lib/$ yalc push   //this will updated all projects that use your "lib"

project/$ npm install 

Pack and deploy with Docker使用 Docker 打包和部署

tar -czvf <compresedFile> <directories and files...>
tar -czvf app.tar .yalc/ build/ src/ package.json package-lock.json

Note: Remember to add .yalc directory.注意:记得添加.yalc目录。

inDocker:在Docker中:

FROM node:lts-alpine3.9

ADD app.tar /app

WORKDIR /app
RUN npm install

CMD [ "node", "src/index.js" ]

用纱线它可以做到

yarn add file:../somelocallib

I wanted to use a set of local dependencies written in TypeScript, and none of the answers here worked for me.我想使用一组用 TypeScript 编写的本地依赖项,但这里的所有答案都不适合我。 npm install would simply refuse to build the dependencies. npm install只会拒绝构建依赖项。

I had to resort to using tsconfig.json to add the packages to my project without marking them as dependencies.我不得不求助于使用tsconfig.json将包添加到我的项目中,而不将它们标记为依赖项。 My usecase is further complicated by the fact that some dependencies depend on each other, and I wanted all of them to come from the local folder.我用例是由事实进一步复杂化,一些依赖互相依赖的,我想他们来自本地文件夹。

Here is my solution:这是我的解决方案:

// tsconfig.json
{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@tiptap/*": [
        "tiptap/packages/*/src"
      ]
    }
  }
}

In the example above, I have a local project subfolder named tiptap/ and there are many packages in tiptap/packages/* .在上面的例子中,我有一个名为tiptap/的本地项目子文件夹,并且在tiptap/packages/*有很多包。 The "paths" option will rewrite all @tiptap/foo imports into ./tiptap/packages/foo/src , across both my own files and the files in tiptap/ . "paths"选项会将所有@tiptap/foo导入重写到./tiptap/packages/foo/src ,包括我自己的文件和tiptap/的文件。

It's not a good solution, but it is the only thing that worked for me.这不是一个好的解决方案,但它是唯一对我有用的方法。

In 2021 you need to use it like:在 2021 年,您需要像这样使用它:

npm i my-pkg@file:./path-to-my-pkg.js

Use .js in the end if its file OR path to folder if its complete package with package.json .如果它的文件或路径到文件夹,如果它是带有package.json完整包,则最后使用.js

Usage用法

const myPkg = require('my-pkg')

That works like charm!这就像魅力一样!

Complete local development guide for yarn users:完整的纱线用户本地开发指南:

First add dependency to your main project:首先将依赖项添加到您的主项目:

cd main-project
yarn add file:../path/to/your-library

Next, if you want to avoid re-building this dependency every time you change it's source:接下来,如果您想避免每次更改它的源时都重新构建此依赖项:

cd your-library
yarn link

This will register a link to your-library.这将注册一个指向您的图书馆的链接。 Next, use the link you just created in your main project.接下来,使用您刚刚在主项目中创建的链接。

cd main-project
yarn link your-library

Now every time you change code in your-library, you don't need to rebuild it and it will automatically be included in your main-project.现在每次你在你的库中更改代码时,你不需要重建它,它会自动包含在你的主项目中。 Yarn link works by creating symlinks in your node_modules folder, read more about it here: https://classic.yarnpkg.com/lang/en/docs/cli/link/纱线链接通过在您的 node_modules 文件夹中创建符号链接来工作,请在此处阅读更多相关信息: https://classic.yarnpkg.com/lang/en/docs/cli/link/

use workspaces使用工作区

the disadvantage using the file:../path/to/your-library is that you either have to npm install or using npm link in order to to the changes to take effect in the packages that import your package.使用file:../path/to/your-library的缺点是您必须npm install或使用npm link才能使更改在导入您的 package 的包中生效。

if you using pnpm: a better solution is using workspace: protocol : workspace:../path/to/your-library .如果您使用 pnpm: 更好的解决方案是使用workspace: protocol : workspace:../path/to/your-library it will symlink the directory to your node_modules rather than copying it, so any changes at the source immediately take effect.它会将目录符号链接到您的 node_modules 而不是复制它,因此源代码中的任何更改都会立即生效。

for example:例如:

  ...
  "dependencies": {
    ...
    "my-package": "workspace:../../dist"
  },

note: this solution is intended to be used in a workspace, so you may need to create pnpm-workspace.yaml (even an empty one) file in the root of your project.注意:此解决方案旨在用于工作区,因此您可能需要在项目的根目录中创建pnpm-workspace.yaml (即使是空文件)文件。

Using Module Alias使用模块别名

  1. Install the module-alias package:安装模块别名 package:

    npm i --save module-alias

  2. Add paths to your package.json like this:像这样将路径添加到您的 package.json:

    { "_moduleAliases": { "@lib": "app/lib", "@models": "app/models" } }

  3. In your entry-point file, before any require() calls:在您的入口点文件中,在任何 require() 调用之前:

    require('module-alias/register')

  4. You can now require files like this:您现在可以要求这样的文件:

    const Article = require('@models/article');

file: is directly linking a local folder as the package. If the local folder contains devDependencies it will cause version collisions. file:直接链接本地文件夹为package。如果本地文件夹包含devDependencies ,则会导致版本冲突。

link: is also linking the local folder as the package, but also ignores linked devDepedencies, behaving exactly as a package consumed from NPM, but locally. link:也将本地文件夹链接为 package,但也忽略链接的 devDepedencies,其行为与从 NPM 使用的 package 完全相同,但在本地。

TLDR: use link to avoid devDependency conflicts:) TLDR:使用link来避免devDependency冲突:)

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

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