Specify depdencies using relative/absolute paths #1558
Comments
I'm unsure what this gives you that |
@isaacs |
I'd really like to use a private git repository for this, but for corporate bullshit reasons I can't. Npm link would work, but it would add another step towards deployment for something that seems like it should be reasonable to do within package.json. I can understand if you don't want to support this, but I feel like it would make sense to have relative/absolute paths be valid package location identifiers. |
Right, but you can always have a global location in your home dir or something, or use sudo. If you set it up to install a local folder, then it means a) it won't work on any other machine, and b) it will just install whatever's in that folder right now. Why not just have the |
@felixge Could you just bundle it with the thing that depends on it? It wouldn't be hard to add support for it. Right now, the logic is:
So, the logic would change to:
since install already supports tarballs and folders. However, this is not supported on purpose, because of the fact that it would mean the package can't be installed on other machines, which opens up a whole pile of annoying deploy-time breakages. Since you'd have to get the dependency on the machine first to make this work anyway, why not just put it there in the node_modules folder yourself, or submodule it, or install it and package it up as a |
If we were to add this (ie, if there's a use case that those other approaches actually don't support for whatever reason), then I'd like to maybe have |
People can write environment dependent package.json's already by specifying wrong urls, private git repos, or forgetting to include something. I don't think relative / absolute paths will make this better or worse. So while enforcing
Well, I was planning to just put the package folder inside the same git repository the company is already using. Using git submodules is (besides the pain, oh the pain!) not an option as getting another repository would require physical paperwork and weeks of waiting time (I kid you not : /). Anyway ... there are certainly workarounds available to me, so this is in no way critical. I was just surprised that relative/absolute paths are not working considering that they do work for |
Ditto @felixge on his most recent comment...minus the physical paperwork, whoa. Regardless of whether you use submodules or folders, the core of the problem I think is requiring
It's pretty awesome to be able to just pull down the repo and submodule and run My current workaround for that example is overriding the global prefix option in the preinstall script: |
@isaacs in our company we can not install git on production servers. The only thing we can do is to prepare independent archive (tar.gz) that could be exploded on production servers. Gcc is not installed on production so every node_modules has to be present and compiled in the archive. Our packages are private and have many dependencies on other private packages, and it would be useful to describe those dependencies with relative paths.
This is what we are doing. In the package.json we add a custom key : myCompanyDependencies, and a build script iterates on those dependencies to
We can't use git
This is what we did (item 1)
The operation team do not want global install for applications. And applications do not have root access.
It's great for development but for production our packages have to be independent. So we have something working with a custom build script but it would be easier for us, to use relative paths in the package.json dependencies. It's not a problem for us to force I am willing to do the pull-request if you are interested in this feature. (BTW you gave all the solutions to make it :) ) |
You don't need git on production servers in order to get benefit from checking your modules into git.
This way, you have git's byte-level tracking of dep sources, and npm's convenience of fetching and managing deps, but without needing git or npm to be on the production machine. |
You could also just drop your stuff in a node_modules folder somewhere above where your app is running, if you want to update deps piecemeal on an integration/staging machine. The node module system is incredibly flexible. There's very little you can't do with it. |
Basically what we do for production is:
For dev we use npm link, it's easier. But with relative/absolute implemented in npm, it would become :
And I wrote you a simplified version of our real script :) We can't use |
Manually adding libs in node_module provoke errors when using shrinkwrap.
All the private libs that I manually copied in node_module are rejected. |
Shrinkwrap only works on modules that can be installed from the registry. It doesn't bundle your deps for you automatically. Do this:
Then shrinkwrap will work. |
Ok thanks |
I'm late to the party here, but saw this labeled I'd love to see this too, though I can share what we do to work around this today -- we just check symlinks into node_modules. E.g.:
Then the code in both components can just I too can't But it'd be nicer if I could include the dependency in package.json (can I still, e.g. w/ bundledDependencies?). And it'd also be a bit more convenient if I could just At the end of the day, this isn't critical -- there are workarounds -- but it'd certainly be convenient. Thanks for the consideration! |
sounds similar to what I want in #2442 there. Without require.paths (or forcing NODE_PATH on people) we cant have private modules that don't live in a registry. Today I forgot to un-gitignore one in ./node_modules and did a |
I would also love a solution that doesn't require running an internal git server, internal npm repository, or adding tarball creation as a deployment step. We have a project layout for our internal code like this:
Where app1/2/3 all need to require('baselibrary'). Right now we're using relative paths for the require statements, ie require('../baselibrary/index') but it's a pain to make sure each statement has the correct relative path. Being able to require('baselibrary') like a normal npm module would be a big win. |
For posterity, I resolve this using the following technique: Our directory structure:
The Problem:Our code is riddled with very brittle ugly relative requires, like the following from a service's lib directory ( var UserModel = require('../../../lib/model/user') Our goal:
Steps to success:
// /server/package.json
"scripts": {
"postinstall": "npm link ./lib/; npm link ../shared/;",
"postupdate": "npm link ./lib/; npm link ../shared/;"
} Result:var UserModel = require('lib/model/user'),
SOME_CONST = require('shared/constant/someconst') |
Fiddling with the permissions like that seems like a complicated and less sane version of |
@nathan7 Thanks for tip. We should probably be using prefix instead of chown'ing the install directory to avoid sudo. Also, thanks for the tip on https://npmjs.org/doc/install.html:
It seems that One option listed here, a preinstall hook with Ideally there would be a more elegant/declarative style to address internal dependencies other than manual management, globals or relative require soup. |
FWIW... The way I solved this need was to add a "links": [
"../moduleAlpha",
"../moduleBravo"
], Then, since I'm already using Cake, I changed my task for updating my module dependencies to be: task 'update-modules', 'update dependencies from npm', ->
shell [
'rm -rf node_modules'
'npm update'
]
fs = require 'fs'
links = JSON.parse(fs.readFileSync('package.json', {encoding:'utf-8'})).links
for link in links
shell "npm link #{link}" (shell is a little function which runs shell scripts and displays their output) The extra |
I don't mind if Today we allow for a url to a tarball, and we allow publishing to npm even if that url is behind a firewall or 404's. So allowing local file urls won't be any worse than our current behavior. This will help us with tests that require npm installing built-on-the-fly packages. Today we have to tarball those packages and serve them via a local connect http server just because npm won't allow us to give it a relative path in the package.json. |
Why not give require support for paths like '//somewhere/from/root'? The "//" would mean from root (root being the directory where the process was started, the initial module). Then anywhere in an app, you could have the same base paths. Alternative paths for these "basepaths", could be ".../lib/someModule". This way, I could have a lib folder at the root directory of my project, and I could use require('//lib/mylib') from anywhere to get the same module. |
So, good news: @dylang went ahead and implemented this feature, more or less, in #5629. Because it's new, it's still a little rough around the edges, but if you read the docs ( Thanks to everyone for their input and (three years' worth of) patience! |
Please provide an example how and where I can specify local dependencies: this doesn't work for instance: devDependencies {
"foobar": "."
} And this installs the last version via npmjs.org devDependencies {
"foobar": "./."
} using npm 2.1.3 |
@timaschew from the docs:
There is one additional restriction, in that you should only use those paths at the prompt: |
Not sure, if this topic is exactly what I need, but here is my problem:
Now the problem is, that if I do |
@hdf You're describing an unrelated question. In the future, or if this reply isn't enough to answer the question, please go ahead and open a new issue. The If the issue is that If I'm misunderstanding your need, please do create a new issue by clicking the green "New Issue" button in the upper-right corner of this page. Thanks |
It would be nice if npm allowed specifying a relative / absolute path for dependencies.
For example:
Given a few pointers, I would love to submit a patch myself this time if you're interested in having this added.
The text was updated successfully, but these errors were encountered: