How to install private git library to npm project? - npm

In a project which I work with there is private project library which is available for me directly - https://gitlab.some-project/some-utils
In package.json it is requested like
"dependencies": {
"#some-project/some-utils": "0.1.42",
...
}
This project wasn't uploaded to npm. I created .npmrc file and put there:
#8some-project:registry=https://gitlab.some-project.com/some-utils
But when I try to "run yarn #some-project/some-utils" I get:
error Received malformed response from registry for "#some-project/some-utils". The registry may be down.
What I am doing wrongly? How to install this library to another project?

I just raised npm version and it works now

Related

npm access ls-collaborators is not reading from the custom registry in .npmrc

I have a custom .npmrc file as follows
#foo:registry=https://gitlab.com/api/v4/packages/npm/
// This works and it returns the latest dist tag from the registry as configured in .npmrc
npm view --json #foo/my-package dist-tags
// This fails with a 404
npm access ls-collaborators #foo/my-package
// npm ERR! 404 Not Found - GET https://registry.npmjs.org/-/package/%40foo%2Fmy-package/collaborators?format=cli - Package not found
Some more context:
I am trying to publish an npm package to a private package registry on gitlab using the np module. These commands seem to be executing as part of one of its steps for user authentication.
What am I missing?
Make sure "private": true is in your package.json file. I was running into a similar problem as you, and adding this fixed it for me.

You cannot publish over the previously published versions

I've updated my package using npm version minor to go from 0.4.0 to 0.5.0, and both package.json and package-lock.json reflect this. However when I run the npm publish command it says:
You cannot publish over the previously published versions: 0.4.0
Is there another place I need to update the semver in order to publish?
This helped me:
Open Command Prompt and do the following steps.
npm version <new_Version_No>
npm publish
In your package.json, there might exist a publish script command with content of npm publish ..., remove or rename the publish command in your scripts of package.json if there is one.
Take the following code for example, this scripts.publish command will again be triggered by npm publish --access public, running recursively.
"scripts": {
"publish": "npm publish --access public" // this was being triggered by running `npm publish`
},
take a look at your package.json.
Is the version actually set to 0.5.0?
If not consider setting it manually there. NPM is telling you that you already have a version 0.4.0 and it cannot publish it again. So it seems to think that it's still on 0.4.0.
npm version [patch|minor|major|<version_no>] should be done to bump up the version and then
npm publish for public visibility add --access public
This should do it.
It happens when there is already npm module with same version.
We need to increment the npm module version and publish it again.
For some reason I was getting this error when I was trying to increase my version from 0.0.0 to 0.0.1. However, updating from 0.0.0 to 0.1.0 worked just fine.
You can try the following:
Update npm version to latest.
cd into the parent folder (of the folder containing package.json i.e. cd into A if your files are A/B/package.json) and then run the command npm publish B.
Doing both fixed the issue for me.

Using .npmrc with npm publish actions

We have a private NPM registry to which we are publishing our packages to, and have a publishConfig section in our projects package.json file which juts contains our registry url which gets picked up by our npm publish commands:
"publishConfig": {
"registry": "xxxxxx"
}
I would like to have this registry url read out from am .nprmc file rather than the package.json.
I have tried doing this, but when using npm publish I get:
400 - Repository with ID='xxx' is Read Only, but action was 'create'!
I figured this may be because I hadnt added my user details to my npmrc, which I have now done, but the problem still remains.
Is it possible for npm publish to use the details from an npmrc specifically?
Just add the following to your .npmrc
registry=YOUR_REGISTRY

How to install npm own local packages?

I have 2 projects(packages) in npm, I want to inject package_A as dependency to package_B. In package_A root folder, I run npm install -g, then npm install it to C:\Users\Myuser\AppData\Roaming\npm\node_moduls\package_A folder. Now in packages.json in package_B I add "package_A": "1.0.0" in dependencies. When in package_B root file I run npm install, its failed package_A#1.0.0 not found.
How can I identified npm to its my own local package?
Notes:
We are a team, then I don't want to address package_A explicitly.
We are using nexus repository manager.
I don't want to publish my projects to http://registry.npmjs.org/.
I'm not 100% clear what you have tried. If you are going to use a custom module for another application you are developing, installing globally won't do the trick. You have to publish that module in npm.
Check this link for more info on publishing in npm
If you have completed the steps correctly, and still no good happens, please check your naming of the module in package.json file.
Instead of typing in the name and version number in package.json file and then npm install, try directly installing in the terminal with --save so that it will automatically be added to package.json file with correct spelling.

React Native: npm link local dependency, unable to resolve module

I am developing a button ui package for react native. I try to build an example project to test this button. The directory structure is as follows:
my-button/
package.json
index.js
example/
package.json
index.js
I try to use npm link:
cd my-button
npm link
cd example
npm link my-button
In example/node_modules/ I can see my-button symlink, VSCode also can auto complete function in my-button package.
But execute example app will show error:
Unable to resolve module my-button ...
Module does not exist in the module map or in these directories: ...
But the path in the error message is correct.
Don't know where I was wrong, or in React-Native have any special way to deal with link local dependency?
I also tried npm install file:../.. It works fine in this way, but not easy to update dependency in example/ after I edited my-button.
The npm link command doesn't work because React Native packager doesn't support symlinks.
After a little research, I discovered that there are two ways to go about it.
Use haul packager in the example app. Haul supports symlinks, so you can use npm link as usual.
Use local dependency via file:../ and then edit files in node_modules folder or reinstall every time you make changes.
I found Haul to work great for this use-case and even set-up a little starter project that also includes storybook, which is really helpful if you have many components to switch between.
Try wml (https://github.com/wix/wml)
It's an alternative to npm link that actually copies changed files from source to destination folders
# add the link to wml using `wml add <src> <dest>`
wml add ~/my-package ~/main-project/node_modules/my-package
# start watching all links added
wml start
I couldn't always make it work with yarn link. What i found extra useful is yalc:
First install it globally once forever:
npm install -g yalc
In the local library/package (i'll call it my-local-package), and run:
yalc publish
Then in your project which uses my-local-package as a dependency, run:
(if you already have added it with any other way, first uninstall it (npm uninstall -S my-lockal-package)
yalc add my-local-package
npm install
If my-local-package is a native module, then run react-native run-android to link the dependency. (or run-ios)
If you make any change in the my-lockal-package, then:
cd path/of/my-local-package
yalc push //updates the local package
cd path/to/my-project
npm install
react-native run-android (or run-ios)
In case the update hasn't been applied, try to cd android && ./gradlew clean && cd .. and then rerun: react-native run-android.
I'm having the same issue while developing a native module wrapper around an existing native SDK. At first I followed #aayush-shrestha's suggestion to install the package locally. Like this:
npm install ../<package-folder> --save
This works as long as I reference the module via NativeModules. Import it:
import { NativeModules } from 'react-native';
And then access a module called ActualModuleName like this:
NativeModules.ActualModuleName
But it fails when I attempt to import the module by name:
import { ActualModuleName } from 'react-native-actualmodulename'
To make that work I had to first pack the package. Run this in the package's root directory:
npm pack
This generates a gzipped tarball:
react-native-actualmodulename-1.0.0.tgz
Now install that in your app:
npm install <path/to>/react-native-actualmodulename-1.0.0.tgz
An enormous downside to this is that you have to re-pack the package every time you make a change to the module. The only workaround I know of is to modify the package's files in node_modules directly and then copy those changes back to your repo when you're done.
But the upside is that your app's source can import ActualModuleName the same way you'll import it once it's released via npm; no environment-specific code necessary.
You can use npm link using Metro. Just add the source folder of the linked package to watchFolders in your metro.config.js.
Ran into the same problem. While I could not make npm link work as it should, I worked around it by installing the local package in the project folder
npm install ../<package-folder> --save
This will install the package like a regular package but from the local folder.
The downside is that the changes you make on the package will not be reflected. You will have to npm install after every change.
Change your package.json
//...
"dependencies": {
//...
"my-button" : "file:../"
},
//...
I also came across this problem. After visiting the below link, I came to know that react- native does not support symlinks.[Click here][1]
However, I have solved this by adding these lines in the metro.config.js file. Please replace your_module_name with your module name.
const path = require('path');
const thirdPartyPath = path.resolve(__dirname + '/../your_module_name/'); // Path of your local module
const thirdParty= {
'your_module_name': thirdPartyPath,
};
const watchFolders = [ thirdPartyPath];
module.exports = {
// existing dependencies
resolver: {
thirdParty,
},
watchFolders
};
I ran into the same problem.
I tried to install a local module using npm, and kept running into the issue of not being able to resolve the module, even though I could see the folder in node_modules and autocomplete of class and method names worked.
I was able to bypass it by installing the local library using yarn instead of npm after seeing this open issue on github. Issue was opened September 2020 and no comment from Facebook as of yet.
This work for me:
step 1 go to package:
npm link packageNameHere
This will link this package to global node_module
step 2 go to directory which you want to use this package and run these
npm link pathToPackageDirectory
npm install pathToPackageDirectory
ex: npm link ~/myDemoPackage
This will link global node_moudle to this project
If you want to import package to file, USE FILE PATH INSTEAD OF PACKAGE NAME !
ex:
my package name is stripe-api-helper. my code are in src/index.ts
then I need to resolve like this:
import { postStripe, Item } from '#aliciaForDemo/stripe-api-helper/src'
if u use '#aliciaForDemo/stripe-api-helper' it will fail.
Could never get my own environment working using any other suggestions, but found a hack that works well (though not ideal) that can be easily set up in just a few lines of code and without changing your RN project configuration.
Use fs.watch for changes recursively in the directory where you're working on your library, and copy the updates over whenever there's been a change:
import fs from 'fs'
const srcDir = `./your-library-directory`
const destDir = `../your-destination-directory`
fs.watch("./src/", {recursive: true}, () => {
console.log('copying...')
fs.cp(srcDir, destDir, { overwrite: true, recursive: true }, function() {
console.log('copied')
})
})
For those still looking for a simple solution without other dependency, try this:
yarn --version
1.21.1
npm --version
6.13.4
Install in project root
cd my-button
yarn install or npm install
register linking in my-button
yarn link or npm link
Install example project
cd example
yarn add ../ or npm add ../
link to my-button
yarn link my-button or npm link my-button
complete pod installation (if necessary)
cd ios
pod install
Try to run
npm run watch
inside the button package. Currently, I'm using this to apply changes from the library to my main project. Please let me know if it works!