Why does “npm install <package_name> —save or yarn add <pakage_name> “ rewrite node-modules files? - react-native

In my project I had changed some file inside the nodemodule folder. But after I installed new packages , my previous changes are removed. All the modules are overwritten

Welcome to Stack Overflow. If you want to make changes to a npm package, you need to clone it to a different location. But first you probably want to clone the repo on github.
Let's assume that you have a github account already, and are logged in. If you choose a repo, like https://github.com/lever/planer You will see a fork button near top right. Click this and it will fork the repo under your account. Then go to your copy of the repo, and click the clone or download button. Copy the repo name to the clipboard.
At your command line, cd to a suitable location, such as ~/projects and type this
git clone https://github.com/<yourname>/planer
cd planer
sudo npm link
npm install
This will copy the repo to your local environment, and the npm link command tells npm that this repo is the one to use for planer. One more step is to cd to your project where you are using planer, and type this command:
npm link planer
At this point it is linked into your project, and you can modify the code as you like (in ~/projects/planer) and test your app. You may need to build the package each time so that your changes go through.

Related

Is there a way to install an npm package locally but not affect package.json or package-lock.json?

I have a project that I'm working on for a client where I have two private packages (which I can't get access to npm install) are inside the package.json.
I do however have access to clone the repos for those said packages. If I simply run an npm install I'll get a permission denied error. Same if I run npm link to the packages.
I've been working around this by removing the packages from the package.json then running npm install ../some-package. This works but isn't a great solution because if I wanted to add a new package I'd have to deal with a bit of a mess with the package.json.
Is there a better way than this?
I have tried running npm link ../some-package but I still get access denied. The only way I've managed to complete an install is by removing the packages then installing them from a local dir.
I don't know the details of your situation, but I see at least two potential solutions to explore.
Option 1: Install the package from the repo
I do however have access to clone the repos for those said packages.
You can install from a git repo and package.json will record that git repo as the source of the package rather than the npm registry.
From the docs at https://docs.npmjs.com/cli/v8/commands/npm-install:
npm install :
Installs the package from the hosted git provider, cloning it with git. For a full git remote url, only that URL will be attempted.
Option 2: Install from the local file system with --no-save
If that approach doesn't work for you, you can try npm install --no-save ../some-package as a build step. The --no-save makes it so it doesn't modify package.json.

Automatically downloading npm packages listed in package.json file

I'm working on creating a local repository that will contain all packages I use in my project, so I can have those packages installed on a machine that does not have access to the internet. I think of the repository that I could clone on the machine and run yarn install to have all the packages available in the project from the local repository. How can I do that? Similar question was asked here Using npm how can I download a package as a zip with all of its dependencies included in the package
There's not enough information in your question to fully understand your situation, but if you commit your node_modules directory to the repository, the modules will be there without the user having to run npm or yarn to install them. This assumes the user will run code from the repo workspace and that there aren't any modules that require a compilation step or other build step that may be platform-specific. But if they're all plain ol' JavaScript modules, you should be fine.
If you want to have all the modules as a separate repo rather than checking in node_modules, I can offhand think of two ways this might work.
Have the packages repo be a check-in of a fully installed node_modules directory. Then make that repo a Git submodule of the main repo that gets cloned as node_modules in the main repo.
Use npm pack to create .tgz files for each package you need. Store those files in the packages repo. Clone that repo into a known path on your target machine. Have the main repo install via path names. For example, if you run npm install /var/packages/foo-1.0.0.tgz, it will add a line to your package.json that might look something like this: "foo": "file:../../../var/packages/foo-1.0.0.tgz". In that case, npm install will install from that path rather than over the network.

Install React-Admin package from specific branch or with open pull request

I am having difficulties trying to install a development branch of React-Admin packages with NPM in an active project, specifically:
ra-tree-ui-materialui
ra-tree-core
To have the changes made in this PR https://github.com/marmelab/react-admin/pull/3379
Is there any way of doing this in a similar way to how you normally would put this in package.json ("username/repo#branch")
It is difficult to install a local version of one of React Admin's package, because we use a mono-repository that contains all the packages.
I see two solutions to your needs.
Install the alpha builds
The core team had just published an alpha for the next version of React Admin. It's not stable yet, but you can try it by running :
npm install --save ra-tree-core#next
npm install --save ra-tree-ui-materialui#next
Install a local version for development
If you want to tweak the React Admin packages while you are using them, you can fork the whole repo and use symbolic links.
# On a separate folder
git clone git#github.com:marmelab/react-admin.git
cd react-admin
make install
make build
cd packages/ra-tree-core
npm link # This will make this package available for linking
And on your project, then run:
npm link ra-tree-core
This will create a symbolic link between your local ra-tree-core and your node_module folder.
I showed these examples with npm, but yarn link works too.

update the version in package.json without clean git working directory (without a task runner like Gulp)

When running a: npm version prepatch I get the error: "Git working directory not clean." And then a list of files that aren't committed yet.
However, I'd like to do this prerelease to test some stuff locally using a private npm registry. Meaning that I don't have to commit the files just yet using Git.
Is it possible to update the version in package.json without clean git working directory?
From the npm version documentation at https://docs.npmjs.com/cli/version:
If run in a git repo, it will also create a version commit and tag. This behavior is controlled by git-tag-version (see below), and can be disabled on the command line by running npm --no-git-tag-version version. It will fail if the working directory is not clean, unless the -f or --force flag is set.
I'm not 100% certain whether you just need --no-git-tag-version, or if you'll also need the --force flag.
You can use git stash.
E.g.
git stash
npm version patch
git stash pop
This will reset your working directory temporarily (remove uncommitted changes). Then you can run npm version {major|minor|patch}. Afterwards, using git stash pop will re-apply your uncommitted changes to your working directory.
Tutorial: https://www.atlassian.com/git/tutorials/saving-changes/git-stash#stashing-your-work
Try to commit first
git add . && git commit -am "new version"
and then
npm version patch

How should I set up a private npm registry?

For a company project, I'd like to set up a private npm registry using Artifactory or Nexus, so packages can be shared with everyone without publishing them on https://registry.npmjs.org/
In maven I would set up a release and a snapshot repo. For resolving I would put them in a group alongside a proxy of maven central.
How does a setup for npm look like? npm's semver is able to differentiate release and prerelease, but I assume that routing them to different registries could be quite a difficult task.
On the other hand one might want to be able to have control over what gets pushed to the "release registry", implementing permissions accordingly. For this you would have to use a "prerelease" and a "release" registy.
according this link. How to set up a free private npm registry… for Windows
you can use the Verdaccio.follow this:
Download the ‘Current’ version of NodeJS .
Install Python. Open Powershell as an Admin and run
npm i -g --production windows-build-tools
then Run
npm i -g node-gyp.
Get the Python.exe file path.
Verify where Python was installed, typically it is stored in the following folder
C:\Users\user-name.windows-build-tools\python27
copy the path + \python.exe.
Add a new Environment Variable.
open the File Explorer and right-click on Computer and select Properties.
Select Advanced system settings and then select Environment Variables.
If PYTHON is not listed under System Variables select New.
Add PYTHON as the variable name and the saved path from Step 3 as the variable value -> Select OK.
Installing / Configure Verdaccio
Open up a command prompt and run npm i -g verdaccio.
Verify the install for Verdaccio was successful.
Open up a new command prompt and run verdaccio You should see the following output.
Open up your favorite browser to localhost:4873
more info:
www.verdaccio.org/docs/en/installation
www.npmjs.com/package/verdaccio
I hope is useful.
If I understood you correctly you would like to have the ability to have a "release" and "snapshot" repository for NPM same as you have now for Maven.
If this is indeed the case then what you can do in Artifactory is to set 2 repositories, one for the "prerelease" and another one for the "release" and aggregate both under one virtual NPM repository. As you mentioned you can have a different set of permissions for each repository and therefore control who can deploy/resolve dependencies and also have the ability to move artifacts from one another.
Hope that answers your question.
In terms of how to do this in Nexus Repository if you really need to, I'd suggest setting up:
npm-release (npm hosted)
npm-prerelease (npm hosted)
npm-all (npm group)
Make the group include both members (so you can install using one URL), and then in your build script or whatever you are using to publish into Nexus Repository, just use the --registry flag to specify which repository you want to publish in to, something akin to this for a "release":
npm publish --registry http://localhost:8081/repository/npm-release/
And this for a "prerelease":
npm publish --registry http://localhost:8081/repository/npm-prerelease/
Here are the npm docs for Nexus Repository 3.x if you need some more help: https://books.sonatype.com/nexus-book/reference3/npm.html