Local dependency in package.json

后端 未结 12 1207
被撕碎了的回忆
被撕碎了的回忆 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条回答
  • npm >= 2.0.0

    This feature was implemented in the version 2.0.0 of npm. 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).

    npm < 2.0.0

    Put somelocallib as dependency in your package.json as normal:

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

    Then run npm link ../somelocallib and npm will install the version you're working on as a symlink.

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

    Reference: link(1)

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

    This works for me.

    Place the following in your package.json file

    "scripts": {
        "preinstall": "npm install ../my-own-module/"
    }
    
    0 讨论(0)
  • 2020-11-22 17:21

    This is how you will add local dependencies:

    npm install file:src/assets/js/FILE_NAME

    Add it to package.json from NPM:

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

    Directly add to package.json like this:

    ....
      "angular2-autosize": "1.0.1",
      "angular2-text-mask": "8.0.2", 
      "animate.css": "3.5.2",
      "LIBRARY_NAME": "file:src/assets/js/FILE_NAME"
    ....
    
    0 讨论(0)
  • 2020-11-22 17:23

    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:

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

    This feels beyond hacky, but it seems to "work". Got the tip from this npm issue: https://github.com/npm/npm/issues/1558#issuecomment-12444454

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

    Here in 2020, working on a Windows 10, I tried with

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

    Then doing a npm install. The result is that a shortcut to the folder is created in 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.

    Since I don't really want a hard link, I tried using an url instead:

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

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

    This worked for me: first, make sure the npm directories have the right user

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

    Then your in your package.json link the directory

    "scripts": {
     "preinstall": "npm ln mylib ../../path/to/mylib"
    }, 
    "dependencies": {
      "mylib" : "*"
    }
    
    0 讨论(0)
提交回复
热议问题