24

So here is the simple file layout.

/my-module
..package.json
/my-app
..package.json

I want my-app to install my-module locally. I tried this:

"dependencies": {
    "myModule": "../my-module"
}

The option -no-bin-links does not work, it only effects node_modules/bin It creates a symlink and does not install the node_modules. I would like to do one of two things, not use the symlink and install node_modules, or use the symlink and install the modules. Ideally combined with peerDependencies when using no symlinks.

4
  • Have you tried using lerna? github.com/lerna/lerna Commented Apr 24, 2018 at 20:45
  • Nope, this is the first I heard of it. Does it use npm under the hood? Commented Apr 24, 2018 at 20:56
  • Have you tried with "myModule": "file:../my-module"? Commented Apr 24, 2018 at 20:58
  • @jemiloii yes, and it's sole purpose is to resolve the many issues of containing multiple npm packages in a single repo. It helps you with version management, common dependencies and locally resolvable dependencies (which seems to be of most interest to you) Commented Apr 25, 2018 at 10:40

3 Answers 3

50

npm current cannot install directly from a folder without a symlink.

You can get around this by generating a tarball from the folder with npm pack, run from the directory of the package you want to install, followed by npm install <tarball>, where <tarball> is the path to your tarball of the package.

Sign up to request clarification or add additional context in comments.

5 Comments

This is the approach I've found I have to use with npm version > 3 in order to install from a local directory.
I used this approach, but I them symlinked the tgz to my sub folder, and as its still installing the tgz, the package code is still copied 8-)
THANK YOU! I had to use this in order to test the local version of my libs module. It programmatically loads requires based off of a relative path and referencing the module via a relative path installed it with symlinks and the paths were all messed up. Thanks to you, I'm able to test the current version of the project and have it installed with the intended folder structure!
If I could give you 100 points I would. This is the post that finally solved about 12 hours' worth of frustration! THANK YOU!!!
This information is outdated. Since NPM v8 you actually can install from a folder without a symlink. See other answer.
12

Since NPM 8 you can use npm install --install-links ../my-module.

Excerpt from documentation https://docs.npmjs.com/cli/v10/commands/npm-link#install-links

When set file: protocol dependencies will be packed and installed as regular dependencies instead of creating a symlink. This option has no effect on workspaces.

You have to specify this parameter on every npm install or npm update or else the dependency will be replaced with a symlink again.

Because in my experience symlinked dependencies practically never work correctly and are always a pain in the ass I added install-links to the global .npmrc so the dependencies are always properly installed instead of linked.

1 Comment

Thanks, this solution works! My problem is npm install create symlinks and make export default failed. If someone has the same problem then this is a solution. Note that yarn add also does not create symlinks.
-7

If a folder containing a program described by a package.json file you can install npm install <folder> or npm install --save/--save-dev <folder> to update dependent package.json.

2 Comments

Well this helps. I've added it to my preinstall script
This still creates a symlink instead of copying the package to node-modules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.