Changing npm module in the node-modules folder - node.js

I want to do some changes to an npm-module I already installed in node-modules folder ... I mean edit the code of the locally installed module
My question is ... should I re-run my react-native app with every change I made to see that change in action?
If yes...
I there a better and faster way for testing my changes?

If you change a Module Version in node-modules you have to rerun npm install and then start your app up again.
The app has to be rebuild/restarted beacuse it useses a newer Version of a module.
Normal Code changes not depending the node-modules can be refreshed without rebuilding the app with react natives hot reload.

This is not really an answer but more of a suggestion.
It's better that you don't change it inside the node modules directory if you want to reuse it somewhere else. You will also have trouble dealing with updates.
One way to handle it better maybe to fork the repo of the npm package in GitHub and then modify whatever you have to in that repo. Finally instead of installing that package, install your copy by specifying dependency in package.json like
"module-name": "https://github.com/<your userame>/<module repo>
or you could directly install using:
npm install https://github.com/<your userame>/<module repo>
The URL can be either the https or ssh URL of the repo.
More details on installing packages from Github repo
Also, regarding your question, if you use the above method, you will have to re-run your app after updating the package.

Related

How to use one node_modules for all projects

Is there an easy way to share one node_modules folders with all angular projects and thus avoiding to download same dependencies every-time when we create a new project ?
if yes , is the method recommended ( what are pros/cons)
Thanks for your time
Package Managers namely npm and yarn have caching mechanism.
The packages are download for the first time only and when you run the install command for the already downloaded package, they won't be downloaded again.
That being said even if you don't have an internet connection you can still install previously cached packages.
Yarn is particularly awesome on handling this and npm is now catching up.
This solves your problem of downloading the packages again. Do lock your package dependencies version. This might help in using the same node_modules for other projects too. But I don't recommend using same node_modules folder for all projects personally as this might mess the entire project. Check out my article about dependency management in JS apps

Editing an NPM module locally

I would like advice on the best practice for modifying an downloaded NPM module (I am working with Webpack/ReactJS).
I have downloaded a package and I wish to remove it from node_modules and package.json and store it in myapp/src where I can edit the package and have the changes immediately available when I am running Webpack dev server.
I am sure there must be a way to do this but I can't seem to find it and it's driving me up the wall!
Thanks.

Can't build my web application when integrating bootstrap template

I'm totally new to Node.js meteor and all development outside of visual studio.
When I go in the console and add bootstrap like this :
npm install twitter-bootstrap
It gets installed and adds all the bootstrap files in my solution but when I run my application with meteor it says
Process finished with exit code 254
No more information. No errors. If I delete all the bootstrap files, it builds and run just fine. Any idea what might be causing this?
I've tried looking for the exit code meaning but I can't find it for my IDE and I'm a bit clueless as for why simply adding those packages without even referencing them anywhere in the project might cause my application not to run at all.
You can't add npm packages in your project folder like that. It will create a node_modules sub-directory that meteor will treat like any other project folder, i.e., it will interpret all the files in it. That's not what you want. Either do the npm install in a super-directory, or, better yet, use the meteor meteorhacks:npm package (https://atmospherejs.com/meteorhacks/npm):
meteor add meteorhacks:npm
and then add the npm dependency to your packages.json file.
{
"twitter-bootstrap": "2.1.1"
}
But the real question is: why do you need this package? bootstrap3 is already part of the standard meteor packages, i.e., you already have full access to bootstrap, incl. javascript.
You can use atmosphere meteor packages called mizzao:bootstrap-3 by running the commend
meteor add mizzoa:bootstrap-3
Alternatively if you want to use npm packages you must add meteorhacks:npm packages.
meteor add meteorhacks:npm
npm install twitter-bootstrap
You can specify all the required npm packages inside a packages.json file.
{
"gm":"1.16.0",
"twitter":"0.2.12",
"twitter-bootstrap":"2.1.1",
}

Meteor 0.9.0 bundle and deploy throw errors

I upgraded to Meteor 0.9.0 today. I use meteor bundle filename to bundle my application and upload to AWS. When I try to start it on AWS, I get
wrong ELF class: ELFCLASS32
This is a known problem with fibers/bcrypt, so I used to go to bundle/programs/server/node_modules, remove the fibers and bcrypt folders and reinstalling them (npm install).
However, currently the node_modules directory does not exist anymore... When I am trying to start nodejs I get
Error: Cannot find module 'underscore'
I tried to manually add the modules with npm install, and even got the server to eventually run, but the client didn't load and the console error was about
Spacebars undefined
(I don't have the exact Spacebar issue)
I know that Meteor 0.9.0 is very new, but if you have advice I would really appreciate it!
You don't need to remove any folders, simply do this in the bundle directory (output of the untarred meteor bundle):
cd bundle
cd programs/server
npm install
Meteor 0.9.0 makes sure it puts up all the node_modules which don't contain binaries, and creates a package.json for npm install for both bcrypt and fibers
Since you've removed the npm modules in node_modules you may have to recreate the bundle from scratch to get them back.
For the Spacebars undefined issue its very likely that you have a package in your project that is not compatible with Meteor 0.9.0. You can find out which one it is by checking your server logs. Though it is not compatible, your app will still run.
This may be boostrap-3, though it may not be. If it is you can meteor remove mrt:bootstrap-3 and meteor add mizzao:bootstrap-3.
Akshat, you are a lifesaver! Based on your answer I eventually managed to figure it out! For the benefit of others, I want to document what eventually worked:
First, I had to manually remove all the old packages from my Meteor project (seems that it didn't do it automatically). In particular, I removed
iron-router
which was the pre-Meteor 0.9 version and installed
iron:router
which is the post-Meteor 0.9 version. (I had to do a similar process of removing and adding different packages for 2-3 other packages too).
Then, once I bundled and unpacked on AWS, I did what Akshat said about npm install in bundle/programs/server. In addition, I had to manually remove the original bctypt by deleting the folder
bundle/programs/server/npm/npm-bcrypt
as otherwise I got an ELS error
If you're getting this error with Meteor-Up. Updating the package should fix it.
npm update mup -g

How to edit a node module installed via npm?

I'm using the node_swiz module, which in turn uses the validator module.
I want to make changes to the validator module, but I used npm install to install the modules/dependencies.
Can I just make changes to the validator module inside of node_modules, or will that node_modules dependencies be re-created and the latest version gotten when I publish to heroku or next time I run npm install?
The structure looks like this:
myNodeApplication
- node_modules
- swiz
- node_modules
- validator [this is the library I want to edit]
Thanks for the help!
You can edit the file directly, but this would be overwritten whenever npm updates, the best thing to do is go straight to the source.
If the changes affect functionality of the overall module, and may be useful to others, you may want to contribute to the original source on github and look for the change to be implemented.
If this is proprietary functionality that is needed, and would not help the development of the module, the best thing to do is fork it from github and make your changes. You can install items directly from github using NPM, and this method would let you integrate future changes in to your custom version from the original source.
To install directly from github, use the following command:
npm install https://github.com/<username>/<repository>/tarball/<branch>
You can use patch-package to make and persist changes to node modules.
This can be done by first making changes to the package inside node_modules and then running the following command, with <package name> being the name of the package you just made changes to.
npx patch-package <package name>
patch-package will then create a patches folder with a file inside, representing your changes. This file can then be commited to git, and patches can be restored later by running npx patch-package (without any arguments).
Optional step:
Add the following in the script section of your package.json to automatically patch the dependency when you execute "npm install".
"postinstall": "npx patch-package"
I didn't want to publish a new module and I also didn't want npm install to overwrite my changes. I found a solution to both of these issues, but it would probably be better to take #Sdedelbrock's advice. But if you want to do it, here's how:
Edit your package.json file to remove the dependency you want to edit.
Go into your project's /node_modules and move the folder somewhere else in your repository that can be committed. So now /node_modules/dependency is at /dependency
cd into the dependency directory and type npm link
cd into the root of your project directory and type npm link dependency It is important that you do this outside of /node_modules and /dependency
If everything worked, you should now have a symlink that was created in /node_modules/dependency. Now you can run your project to see if it works.
Fork the Github repo and make the necessary changes then you can install the package like
npm install git+https://github.com/visionmedia/express.git

Resources