Peer vs Dependency for npm package - reactjs

I am creating a react-native npm module.
The npm module uses React & React-Native Packages i.e say something like this
import React, { Component} from 'react'
import { View } from 'react-native'
class something extends Component {
render () {
return (
<View>
{/* Code */}
</View>
)
Now, here adding should I add React and React-native as dependencies or peerDependencies and why?
Update: My current package.json for library (npm module)
"dependencies": {
"prop-types": "^15.7.2",
"react": "^16.9.0",
"react-native": "^0.60.5",
"react-native-device-info": "^2.3.2"
}
And For the app let it be something like
"dependencies": {
"prop-types": "^15.7.2",
"react": "^16.9.0",
"react-native": "^0.60.5"
}
Does it look correct?

If your module is something like an ui library and to be installed into another react app, it should list react and react-native as devDependencies and peerDependencies. There should not be 2 versions of react (and react-native) in the host app, so in order to achieve that, use these 2 dependencies, they won't install anything in the host app.
devDependencies: describes the packages that you rely on when developing; you both need react and react-native so put them here; use any versions that you want.
peerDependencies: describes the packages that you require/suggest your host app to use, the versions should at least match your uses, so there could be a minimum version or not limited;
For example check this package.json, when developing, this package uses "react": "^16.8.5" as "devDependencies"; and when being installed, it actually does not use any specific api belongs to certain version of react, so it uses "react": "*", as "peerDependencies".
References:
https://nodejs.org/es/blog/npm/peer-dependencies/
What we need is a way of expressing these "dependencies" between
plugins and their host package. Some way of saying, "I only work when
plugged in to version 1.2.x of my host package, so if you install me,
be sure that it's alongside a compatible host." We call this
relationship a peer dependency.
https://docs.npmjs.com/specifying-dependencies-and-devdependencies-in-a-package-json-file
"devDependencies": Packages that are only needed for local development
and testing.

Related

How to run a react project when react is added as peer dependency

I'm trying to run a repo from Github locally. The folder structure for react is somewhat different what what I usually see in a react project(I'm new to react). the folder structure is like
-index.js
-css folder
-jsx folder - components
-jsx folder - index.html
-jsx folder - index.js
Also react is added as a peer dependency. I'm not sure how to run this repository. Any help is much appreciated
React in package.json
"peerDependencies": {
"react": "^16.3.0",
"react-dom": "^16.3.0",
"react-router-dom": "^5.1.2"
}
It was library files. So I had to create a new project using create-react-app and install the library in my new project and then make changes.

AuthError - Error: Amplify has not been configured correctly

First I have successfully completed configuring my react application using amplify configure. I did that with the help of AWS Amplify docs. Then I have successfully added authentication to my amplify project, using amplify add auth and amplify push. I followed all the steps in the AWS - Authentication with Amplify Doc
My App.js looks like this,
import React from 'react';
import { withAuthenticator, AmplifySignOut } from '#aws-amplify/ui-react';
import Amplify, { Auth } from 'aws-amplify';
import awsconfig from './aws-exports';
Amplify.configure(awsconfig);
const App = () => (
<div>
<AmplifySignOut />
My App
</div>
);
export default withAuthenticator(App);
But when I try npm start, it shows the following error,
I found the solution to this problem in this github-issue
The fix was simple. Amplify docs do not tell you to load configs of aws-exports to Auth module.
Adding this simple line of code in App.js, solved the issue for me.
import Amplify, { Auth } from 'aws-amplify';
import awsconfig from './aws-exports';
Amplify.configure(awsconfig);
// >>New - Configuring Auth Module
Auth.configure(awsconfig);
npm un aws-amplify #aws-amplify/ui-react
npm i aws-amplify #aws-amplify/ui-react
This worked for me. Thanks #Ignacio
I think this problem occurs under various Amplify module versions due to inconsistencies between installed Amplify modules.
In my cases, reinstalling as the below solved it many times.
npm uninstall --save aws-amplify #aws-amplify/ui-react #aws-amplify/ui-components
npm install --save aws-amplify #aws-amplify/ui-react #aws-amplify/ui-components
There is a case that needs reinstalling #aws-amplify/ui-components if you use it.
If you are using Yarn, this issue can arise from a package manager conflict based on how they manage the dependency tree and version updates.
If you are seeing this issue repeatedly; In some scenarios you should try using Npm.
If you are using Yarn -You should first delete Yarn.lock and your node_modules directory.
npm install
Also, see the answer above too Untamables Answer
I was doing todo app in Expo,and faced same issue.
I had to add right path for config file. Path is different for aws-exports and it is not mentioned in Docs.
My example code is below
import awsconfig from './src/aws-exports'
Amplify.configure(awsconfig);
Auth.configure(awsconfig);
import { createTodo } from './src/graphql/mutations'
import { listTodos } from './src/graphql/queries'
import { withAuthenticator } from 'aws-amplify-react-native'
run amplify update auth
choose Walkthrough all the auth configurations.
enable unauthenticated logins along the walkthrough and leave other settings.
Source: https://docs.amplify.aws/lib/graphqlapi/authz/q/platform/js/#using-amplify-graphql-client
When using AWS_IAM for public API access, unauthenticated logins must be enabled. To enable unauthenticated logins, run amplify update auth from the command line and choose Walkthrough all the auth configurations.
this solved my problem in combination with graphQL API
I am dealing with this error right now without having #aws-amplify/ui-react installed. I believe there were changes made to Auth from version 3 to 4 that is causing the issue
If you are following a tutorial here are some precautions that might be helpful to you and save you same headache.
Use modules and dependencies that the tutor of the tutorial is using. Do not assume anything.
in package.json file write the dependencies and version as guided by the tutorial and at the end dont run npm install.
why? npm install pops up ERR errors that are headaches. Use yarn install and it will fix your errors.
e.g
in my project's package.json file i have the following::
{
"version": "0.1.0",
"private": true,
"dependencies": {
"#aws-amplify/ui-react": "^0.2.9-unstable.12",
"#stripe/react-stripe-js": "^1.9.0",
"#stripe/stripe-js": "^1.32.0",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"#testing-library/user-event": "^13.5.0",
"aws-amplify": "^4.3.26",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^3.8.0",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"react-stripe-elements": "^6.0.1",
"stripe": "^9.10.0",
"uuid": "^7.0.0",
"web-vitals": "^2.1.4"
},
}
from the guide of the tutorial.But from react all these dependencies are old some obsolete. What do i do? I dont install each individual to bring confusion in modules by just running yarn install to align them and erradicet errors.

Why my display is different than react-bootstrap example?

I got this example from https://codesandbox.io/s/github/react-bootstrap/code-sandbox-examples/tree/master/basic. Referring to below image:
https://ibb.co/rZPn6L5
I copied whole code App.js and App.css, did a npm start. Below is from my web browser
https://ibb.co/LrP4NR2
Anything I missed ? My display is different that the one showed in codesandbox.
Also copy the imported minified bootstrap CSS file that was placed in index.js
// Importing the Bootstrap CSS
import 'bootstrap/dist/css/bootstrap.min.css';
You likely forgot to copy over the dependencies from package.json and install them.
When you open the package.json in CodeSandbox, you will see this:
"dependencies": {
"bootstrap": "^4.4.1",
"react": "^16.13.1",
"react-bootstrap": "^1.0.0",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1"
},
Do you have the bootstrap and react-bootstrap dependencies installed locally? If not, add this to your package.json:
"bootstrap": "^4.4.1",
"react-bootstrap": "^1.0.0",
I almost forgot; if you open the index.js in CodeSandbox, you will see the following two lines:
// Importing the Bootstrap CSS
import 'bootstrap/dist/css/bootstrap.min.css';
You also need to copy over that code and put it in your local index.js.
Then run npm install or yarn (depending on which package manager you're using) and it should work.
By the way, looking at the URL I could tell that the code is coming from a GitHub repository. You can just clone that repository with Git and and then run the examples locally. That's far better than copying and pasting code from CodeSandbox.

__WEBPACK_IMPORTED_MODULE_4_react___default.a.memo is not a function

I just connect the app with redux and react-redux connect function, together with state and dispatches. It compiled without problems but the results are not showing. And it looks like below.
I tried to find it and found that i have to change react version.
$ sudo npm install --save react#16.4.0 react-dom#16.4.0
But it didn't work.
I am following this tutorial.
https://www.youtube.com/watch?v=BxzO2M7QcZw
you're using wrong version of React, React.memo is introduced with version 16.6.0 so, try this command to install the right version
npm install --save react#16.6.0 react-dom#16.6.0
for more info click here
This had happened to me as well. This happens when dependencies version gets updated, and the dependencies of the YouTube video you are watched has an old version.
Hence, replace the following dependencies in the package.json file:
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-redux": "^5.0.7",
"react-router-dom": "^4.3.1",
"redux": "^4.0.0",
"redux-thunk": "^2.3.0",
and run npm install.
Since the connect() function connects a React component to a Redux store,
react, redux need to be version supported. If you are willing to use newest dependencies please refer the Redux documentation.
The above method should solve your problem.
That's because #material-ui/styles has a peer dependency on react >= 16.7.0-alpha.0 and react-dom >= 16.7.0-alpha.0 that include hooks.
To use #material-ui/styles, change your react and react-dom dependencies like this:
"dependencies": {
...
"react": "^16.7.0-alpha.2",
"react-dom": "^16.7.0-alpha.2",
...
},
Find out which version of react-redux version you are using and then go to https://react-redux.js.org/versions. Click on the documentation associated with your version. You should see something like this:
Installation
React Redux 7.1 requires React 16.8.3 or later.
Update your react in package.json to the appropriate version and install.

Forking react-scripts to add custom dependencies

Create React App docs suggest forking react-scripts (instead of ejecting) if we have many similar projects. That's exactly my use case - I am trying to standardize the packages and structure that I always use in a React project. I would like to add these packages to the generated package.json file as opposed to the dependencies of react-scripts. For example, in the generated package.json file, the dependencies section should look like this:
"dependencies": {
"#material-ui/core": "^3.9.2",
"react": "^16.8.3",
"react-dom": "^16.8.3"
},
It appears that the only way to do this is to modify create-react-app itself because the code that generates package.json resides there.
I know that the CRA team recommends against modifiying create-react-app itself. So is there any other way to achieve what I am trying to do?

Resources