7

Let's imagine I have to develop an npm package, my-package, which needs to be imported and used by my-project.

While developing my-package I need to install and use other packages from npm. At the end of the development of my-package, I bundle all the relevant files in the dist directory. Since I do not want to have all the files in the node_modules folder as part of the package published, I specify in .gitignore (or .npmignore) to exclude node_modules folder. So the final structure of the folder of my-package looks something like

my-package
  dist
    ... // all the code of the package to be distributed
  node_modules
    ... // all the node modules
  src
    ... // all source files
  package.json
  .gitignore

Now I publish my-package on npm repository and then import it in my-project, with the command npm i my-package.

As a result of such import, the structure of the directory hosting my-project is something like

my-project
   ...  // stuff
   node_modules
      my-package
         dist
           ... // all the code of the package to be distributed
         src
           ... // all source files
         package.json

As expected, no node_modules folder is imported under my-package folder.

Since I am the developer of both my-package and my-project, during development I would like to import my-package from the local path with a command which could look like

npm -i ../my-package

If I proceed like this, the result I see is that all the files and folders stored under my-package directory are copied under the node_modules folder of my-project, i.e. .gitignore (or .npmignore) exclusions are not considered. The net result is a structure such as

my-project
   ...  // stuff
   node_modules
      my-package
         dist
           ... // all the code of the package to be distributed
         node_modules
           ... // all stuff imported by my-package
         src
           ... // all source files
         package.json
         ...  // any other file under my-package

Is there a way to install from local path only the relevant files of my-package as it happens if the package is installed from the npm repository?

Picci
  • 16,775
  • 13
  • 70
  • 113
  • 1
    If your main goal here is to develop _my-package_ in tandem with _my-project_ then _perhaps_ [npm-link](https://docs.npmjs.com/cli/link) is a more suitable alternative. Utilizing `npm-link` is explained further in this [answer](https://stackoverflow.com/questions/8088795/installing-a-local-module-using-npm#answer-18778516) and fairly good blog [here](http://justjs.com/posts/npm-link-developing-your-own-npm-modules-without-tears). The pitfalls of `npm -i ../mypackage` are noted in this [comment](https://stackoverflow.com/questions/8088795/installing-a-local-module-using-npm#comment-36002650). – RobC Jan 26 '18 at 10:15
  • added to package.json with { "my-module": "file:local_modules/my-module"}, detail please refer to https://medium.com/@arnaudrinquin/build-modular-application-with-npm-local-modules-dfc5ff047bcc – Yuanpeng.Zheng Jul 30 '18 at 03:15

1 Answers1

4

We had the same problem today. We figure it out by adding to the package.json of the local package

“build”: “yarn run clean && yarn build-components && yarn pack”

And then to the project that is using the local package we added to the dependencies of the package.json

“yourpackage”: “file:../your-tgz.tgz”,

hope it helps

Note: if you are using yarn you might have problems with the cache.

Alfrex92
  • 6,278
  • 9
  • 31
  • 51