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?
You can always copy node_modules and then run npm install or npm update in the new project to make sure you've got up-to-date versions. npm will use the files in node_modules as a cache and should only bring down newer content if required. In short: it won't hurt.
You should not include folder node_modules in your . gitignore file (or rather you should include folder node_modules in your source deployed to Heroku). If folder node_modules: exists then npm install will use those vendored libraries and will rebuild any binary dependencies with npm rebuild .
You should, typically, not upload node modules folder manually. They are the external libraries and are easily available to install separately. So, when moving files through filezilla, move everything but node modules. Then, in your server, simple run npm i before running the application.
Yes you can copy whole node_modules (have done it multiple times) from one project to another and use same package. json and package-lock (Will only save time in dependencies installation/download)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With