21

My goal is to specify what files will be included in my node module before publishing it and be able to test the installation locally. Using the "files" in the package.json works in that if I publish my package and install it, then I get only what was specified in "files".

This isn't the case when I use npm link. Be it "files" in package.json or an .npmignore, npm link always seems to give me every file. How can I test my modules installation locally like this?

Ex:

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

If ~/projects/node-redis had "files: [lib]" in its package.json, you would expect only lib to show up in ~/projects/node-bloggy after running "npm link redis", but this is not the case.

Aside: I love node and npm, but if you look at what is in your node modules, there's so many extraneous files like PNGs used in the readme. Modules are ridiculously huge because of this.

UPDATE:

npm install <path>

seems to respect "files" in package.json according to an answer here and others on stackoverflow. I can't speak for other systems but with npm v 6.9.0 on Fedora Linux, this doesn't work as all files are still copied.

Example:

If you need a published module to test this scenario with, I recently published num2cheque which has no dependencies. You will see that if you install it from the npm registry with

npm install num2cheque

you do not receive the test directory which I have locally because in the package.json I specify

"files": [lib]

Add a test directory to your local install then try to use npm link or npm install with a path and you will see that test directory is now included. Hope that illustrates the issue.

2
  • 4
    I'm running into the same thing. The best work around I have found so far is to do an npm pack on the dependency, and link to its local path in my package.json. But that is pretty slow. Really wish there was a better way to make it look the same for local as it does for production.
    – cschear
    Nov 20, 2017 at 21:33
  • Please vote up answers that are helpful and well-researched. As the asker, you have a special privilege: you may accept the answer that you believe is the best solution to your problem. To mark an answer as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in. Thanks! stackoverflow.com/help/someone-answers Nov 9, 2019 at 18:51

1 Answer 1

15
+50

Workaround: npm install a GIT repo URL

You may want to install a package from a GIT repo, eg

npm install https://github.com/AndreasPizsa/parse-decimal-number.git

This is an actual npm install which respects the files entry, even if the package has not yet been published to an npm repository.


Background

npm link does not copy, it creates a link

npm link does not actually install the package by copying it into the target folder.

Instead it creates a symbolic link to the source folder, which is why you’re seeing all the files that are in the source folder ("node-redis"), not just those specified in files.

This behavior is documented in the npm link documentation:

First, npm link in a package folder will create a symlink in the global folder {prefix}/lib/node_modules/ that links to the package where the npm link command was executed. (see npm-config for the value of prefix). It will also link any bins in the package to {prefix}/bin/{name}.

Next, in some other location, npm link package-name will create a symbolic link from globally-installed package-name to node_modules/ of the current folder.

https://docs.npmjs.com/cli/link.html

"What’s a Symlink?" you may ask:

a symbolic link (also symlink or soft link) is a term for any file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution.

https://en.wikipedia.org/wiki/Symbolic_link

If your concern is the use of space on your hard disk, worry not - nothing is copied or duplicated, only linked (just like linking to Wikipedia doesn’t duplicate it, it actually saves space)

... and so does running npm install locally

npm install with the path to the package will also create a symbolic link to the package in question. A helpful scenario for this can be a module that’s still under development.

cd ~/projects/node-bloggy
npm install ~/projects/node-redis

This will create a symbolic link under node_modules in your node-bloggy project.

5
  • 1
    I have no idea why someone would vote this answer down, it both explains why this happens and how to work around the problem. Nov 10, 2019 at 10:19
  • The purpose of the question is to explain that npm link does not achieve a desired effect which is to reflect an npm install post publish without publishing. I too thought npm install with a path would work, but it does not. I will update with my npm version
    – Arman
    Nov 11, 2019 at 18:16
  • @Arman fair point about the local install. I've tried this on my machine before posting and only looked at the npm console output, not in the node_modules. npm install with a local directory will also just create a link. I will update my answer accordingly. Nov 11, 2019 at 19:23
  • Yep, npm install'ing a git repo actually works like an npm install for a published module! I made some edits to show just that workaround instead of the pretext/background. I'm kind of surprised npm doesn't simply have a command for this in case someone does not have a git repo setup. I (or someone) should propose this to npm
    – Arman
    Nov 11, 2019 at 20:08
  • Using npm pack and installing the .tgz file like "modulename": "/lo/ca/tion/package.tgz" should work as well Aug 17 at 19:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.