Local dependency in package.json

后端 未结 12 1208
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 16:42

I want to do something like this, so npm install also installs the package.json of ../somelocallib or more importantly its dependencie

相关标签:
12条回答
  • 2020-11-22 17:28

    Actually, as of npm 2.0, there is support now local paths (see here).

    0 讨论(0)
  • 2020-11-22 17:30

    I know that npm install ../somelocallib works.

    However, I don't know whether or not the syntax you show in the question will work from package.json...

    Unfortunately, doc seems to only mention URL as a dependency.

    Try file:///.../...tar.gz, pointing to a zipped local lib... and tell us if it works.

    0 讨论(0)
  • 2020-11-22 17:37

    It is now possible to specify local Node module installation paths in your package.json directly. From the docs:

    Local Paths

    As of version 2.0.0 you can provide a path to a local directory that contains a package. Local paths can be saved using npm install -S or npm install --save, using any of these forms:

    ../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. 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.

    0 讨论(0)
  • 2020-11-22 17:38

    Master project

    Here is the package.json you will use for the master project:

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

    There, ./somelocallib is the reference to the library folder as relative to the master project package.json.

    Reference: https://docs.npmjs.com/files/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).

    This is a known bug with NPM.

    Reference: https://github.com/npm/npm/issues/1341 (seeking a more up-to-date reference)


    Notes for Docker

    Check in your master package.lock and your somelocallib/package.lock into your source code manager.

    Then in your Dockerfile use:

    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.

    0 讨论(0)
  • 2020-11-22 17:38

    Two steps for a complete local development:

    1. Provide the path to the local directory that contains the package.
    2. Symlink the package folder
    0 讨论(0)
  • 2020-11-22 17:45

    Curious.....at least on Windows (my npm is 3.something) I needed to do:

    "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.

    I messed around a little more and determined that ../xxx was sufficient.

    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.

    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"; module2 has no local dependency; nodeApp has dependencies "module1": "../module1" and "module2": "../module2".

    Not sure if this only works for me since all 3 folders (module1, module2 and nodeApp) sit on that same level.......

    0 讨论(0)
提交回复
热议问题