40

Before publishing my node library, I could use the advice the npm documentation wrote about:

To test a local install, go into some other folder, and then do:

cd ../some-other-folder

npm install ../my-package

Prior to version 5 of npm, I had no problem as it produce what I expected, ie a folder with the output of what I will publish.

However, using npm 5, it now creates a symlink to my local project as described in the npm documentation:

npm install :

Install the package in the directory as a symlink in the current project. Its dependencies will be installed before it's linked. If sits inside the root of your project, its dependencies may be hoisted to the toplevel node_modules as they would for other types of dependencies.

How can I use the "old" way to install local project? Or is there a new way to check if my library is correct?

Thank you.

3
  • 1
    You can use https://www.npmjs.com/package/install-local npm module. Jul 12, 2017 at 8:16
  • Thanks a bunch @HardikModha :)
    – l-lin
    Jul 12, 2017 at 9:57
  • I tried using install-local, but it also creates symlinks so @HardikModha's solution doesn't work 100%. It also has the undesirable side effect of not installing the dependencies of the local dependencies.
    – Severun
    Dec 6, 2017 at 22:50

2 Answers 2

34

Use npm pack + npm install (as suggested by install-local package)

npm pack <path-to-local-package>
npm install <package-version.tgz>

This will effectively copy your local package to node_modules. Note that this will package only production relevant files (those listed in the files section of your package.json). So, you can install it in a test app under the package own directory. Something like this:

my-package
  package.json
  test
    test-app
      package.json
      node_modules
        my-package

Assuming that test dir is not included in the files in my-package/package.json.

This works the same way with npm 5 and older versions.

2
  • 7
    Is there a better way? In my setup I have all of my packages at the same level in my repo. I specify depenencies by doing npm install --save ../otherpackage in the mainpackage directory. Before it just copied ../otherpackage to mainpackage/node_modules/otherpackage, now it creates a symlink at that location that points to ../../otherpackage. My problem is that when there are symlinks in mainpackage/node_modules, it blows up nodemon w/ an ENOSPC error. Having to create a package with pack, then npm install the tgz file causes a problem for automated deployment.
    – Severun
    Dec 6, 2017 at 21:22
  • 1
    Just tried this and it does not work, as otherpackage has a local dependency on ../yetanotherpackage. I get the error 'Could not install from from "yetanotherpackage" as it does not contain a package.json file.'
    – Severun
    Dec 6, 2017 at 21:29
0

I wrote npm-install-offline which allows you to install npm packages from a local repository or folder. By default it copies the folder on install but you can also choose to symlink. https://www.npmjs.com/package/npm-install-offline

npx npm-install-offline ../some-package

Or

npx npm-install-offline my-npm-package --repo ./my-offline-npm

It also will install the package dependencies which npm does not do with local packages.

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.