Can't resolve 'react-native-web/dist/exports/ViewPropTypes' after upgrading to 0.12.1
kopax opened this issue ยท 57 comments
I have tried to upgrade from react-native-web
v0.11.7 to 0.12.1 and it breaks my app:
Module not found: Can't resolve 'react-native-web/dist/exports/ViewPropTypes' in '/home/dka/workspace/test-native/node_modules/react-native-swipe-list-view/components'
See:
Related to:
It was removed as mentioned in the release notes https://github.com/necolas/react-native-web/releases/tag/0.12.0
A lot of other packages are not able to be compiled after upgrading to 0.12.x
We use react-native-web as alias for react-native. So, if react-native still support prop types exports, but as deprecated, i think this package should behave in the same way: support them, but deprecated.
Hello.
I have the same issue like this.
My import is like this
import {Overlay as BaseOverlay, Button, Text} from 'react-native-elements/src/index';
And errors in terminal
./node_modules/react-native-elements/src/config/ViewPropTypes.js Attempted import error: 'ViewPropTypes' is not exported from 'react-native-web' (imported as 'RNViewPropTypes').
I created the react app using create-react-native-web-app
command.
System: Windows 10
IDE: VS Code
"node": v13.6.0
"react": "^16.13.0"
"react-native": "0.61.5"
"react-native-elements": "^1.0.0-beta8"
"react-native-web": "^0.12.2"
What is the solution to fix this issue?
Thanks for your reply.
Hello developers,
I had the same issue. Before, I manually copied the library I was using in my code, but it turns out was not the best way so I created a workaround.
Explanation
If you mock ViewPropTypes in node_modules/react-native-web/dist/index.js the error will go away.
So what I did is I wrote some batch file for Windows, bash for Unix-like, Python for both, it is up to you to decide which one. Just copy and paste.
Workaround
- create a .bat|.sh|.py file depending on your OS anywhere in your app and feel free to customize the mock. For me setting the style to null fixed the issue. In my case, I created a folder called scripts so my file is located at scripts/fix.bat|.sh|.py
Copy & paste the code
Python: fix.py OS-agnostic, use this if possible
import os
import sys
print("โ
Fixing PropTypes issues")
dir_path = os.path.dirname(os.path.realpath(__file__))
rnw_filename = dir_path + "/../node_modules/react-native-web/dist/index.js"
def append_new_line(file_name, text_to_append):
"""Append given text as a new line at the end of file"""
if text_to_append in open(file_name).read():
print("โญ๏ธ Skipping...")
else:
with open(file_name, "a+") as file_object:
file_object.write("\n")
file_object.write(text_to_append)
file_object.close()
# Fix
append_new_line(rnw_filename, "export const ViewPropTypes = { style: null };")
Windows Batch: fix.bat
@echo off
echo 'Fixing ViewPropTypes issues'
REM Fix ViewPropTypes issues
ECHO export const ViewPropTypes = { style: null };>>"PATH_TO_NODE_MODULES/react-native-web/dist/index.js"
Unix-like bash: fix.sh
#!/bin/bash
echo 'Fixing PropTypes issues'
if grep -q "export const ViewPropTypes = { style: null };" ../node_modules/react-native-web/dist/index.js; then
echo "ViewPropTypes fixed already!"
else
echo "export const ViewPropTypes = { style: null };">> ../node_modules/react-native-web/dist/index.js
fi
I added in the bash a cool if-else so that you can run it infinitely without duplicating it.
Please replace PATH_TO_NODE_MODULES with the path to your node_modules folder. For example, if you at your root folder it should be "node_modules"
- (optional) Go in your package.json
Each time you run npm install
| ``yarn install` or when you add a new package, the fix is removed, since the package refreshed. To overcome this, modify your scripts and add a post-install command that is called after.
Basically, it would execute your bat script each time you hit npm or yarn install.
With the python version
"postinstall": "python fix.py"
With .bat or .sh
"postinstall": "fix"
Hope it would help.
It works for me. Don't hesitate to ask me anything.
Thanks!
Franz
Hello @franznkemaka I have the same issue of @hakuna0829 but the script didn't fix my problem, but I'm not sure if I'm making the things good, this is the way that I put that script in the package.json :
....
"scripts": {
"web": "node scripts/start.js",
"test:web": "node scripts/test.js",
"build": "node scripts/build.js",
"start": "react-native start",
"start-clean": "rm -rf $TMPDIR/react-* && watchman watch-del-all && npm start -- --reset-cache",
"test": "npm run test:web && npm run test:native",
"android": "react-native run-android",
"ios": "react-native run-ios",
"test:native": "node_modules/.bin/jest -c ./config/jest/jest.config.native.js",
"postinstall": "autofix.bat"
},
....
and I have put the file autofix.bat in the root of my project.
than you
Hello @willerpp,
it should normally work. May I please see the content of your autofix.bat?
Possible fixes:
-
Did you try to run the script manually via cmd? The script will only run if something is installed. For the first time, I had to run it manually, as I already installed my packages.
-
Open node_modules/react-native-web/dist/index.js. Can you see this at the bottom
export const ViewPropTypes = { style: null };
Thanks!
Franz
Hello @willerpp,
You're welcome and I'm happy it works for you too.
To test the postinstall feature. You can install any package, let say lodash, then check if the script updated it. After that, you know it works and therefore free to remove lodash from your packages.
Thanks!
Franz
Module not found: Can't resolve 'react-native-web/dist/exports/ViewPropTypes' after update to expo sdk 39.0
Hello @aguilared . I had the same issue. Before, I manually copied the library I was using in my code, but it turns out was not the best way so I created a workaround.
Check out my comment above: #1537 (comment)
It works for me. Don't hesitate to ask me anything.
Thanks!
Franz
@aguilared the same
@aguilared the same
Did you try my workaround above, this is because ViewPropTypes was removed from react native, so react native web removed it too.
@aguilared the same
Did you try my workaround above, this is because ViewPropTypes was removed from react native, so react native web removed it too.
Are you sure? I have the same issue, but npm package works without problems on RN.
- @echo off echo 'Fixing ViewPropTypes issues' REM Fix ViewPropTypes issues ECHO export const ViewPropTypes = { style: null };>>"PATH_TO_NODE_MODULES/react-native-web/dist/index.js"
Thank you for your answer. I am sure it can help me. Could you please explain in details what this lines do? I need create a bashscript for my project.
I use react-native-smooth-slider
I have added export const ViewPropTypes = { style: null }
to "PATH_TO_NODE_MODULES/react-native-web/dist/index.js".
But my problem have not been solved. Npm package need something more :
`TypeError: Cannot read property 'source' of undefined
Module../node_modules/react-native-smooth-slider/src/Slider.js
node_modules/react-native-smooth-slider/src/Slider.js:174
171 | /**
172 | * Sets an image for the track.
173 | */
174 | trackImage: Image.propTypes.source,
| ^ 175 |
176 | /**
177 | * The style applied to the thumb.
View compiled
webpack_require
/Users/yue/Project/forProd/RN/TouchWand/code/TW/webpack/bootstrap:785
782 | };
783 |
784 | // Execute the module function
785 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
| ^ 786 |
787 | // Flag the module as loaded
788 | module.l = true;
View compiled
fn
/Users/yue/Project/forProd/RN/TouchWand/code/TW/webpack/bootstrap:150
147 | );
148 | hotCurrentParents = [];
149 | }
150 | return webpack_require(request);
| ^ 151 | };
152 | var ObjectFactory = function ObjectFactory(name) {
153 | return {
....
Hello @karpov-denys,
When you add export const ViewPropTypes = { style: null }
to PATH_TO_NODE_MODULES/react-native-web/dist/index.js
. You mock ViewPropTypes, by exporting an object.
Check out my answer above #1537 (comment)
I updated the answer, now there is a batch script, a bash script, and a python script. I recommend you to use the Python version since it works on both Unix-like OS & Windows having Python installed.
Best regards,
Franz
@franznkemaka have a look at https://www.npmjs.com/package/patch-package as it makes patching stuff like this much easier ๐
@jaulz
Thanks, I'll check it out
Hello everyone :) I am having the same issue as @aguilared. I tried adding export const ViewPropTypes = { style: null };
to /react-native-web/dist/index.js
as @franznkemaka mentioned, but it doesn't change anything. Are there any other ideas for a possible solution?
Hello everyone :) I am having the same issue as @aguilared. I tried adding
export const ViewPropTypes = { style: null };
to/react-native-web/dist/index.js
as @franznkemaka mentioned, but it doesn't change anything. Are there any other ideas for a possible solution?
Hi @Lis-Ma , what is the error message?
In my case ViewPropTypes.style was undefined, so it meant that ViewPropTypes
was not defined that is why I exported ViewPropTypes
in the /react-native-web/dist/index.js
If for example, it says ColorPropType is undefined, then you have to export it too.
It didn't work for me as well unfortunately (it still wanted the export, but in /exports folder), but I've used part of that script and made myself my own workaround here (specifically for react-native-table-component): dohooo/react-native-table-component#131 . It's not the best solution, but will work until the package is upgraded.
Maybe in your case, you also need to add a ViewPropTypes file into /exports folder and declare that there instead (and from index.js, just export it like the rest are)
Hi @Dammic,
it depends on what is in the index.js. If there is already something like this: export { default as ViewPropTypes } from './exports/ViewPropTypes';
then you have to create that file., or simply remove the line. If not you can just mock it by exporting an empty object for example like: export const ViewPropTypes = { style: null };
Hey @franznkemaka, the error message with or without exporting the empty ViewPort object is Module not found: Can't resolve 'react-native-web/dist/exports/ViewPropTypes'
. Also thx @Dammic, I will try out your idea in a minute.
I just tried out @Dammic's solution with node_modules/react-native-web/dist/exports/ViewPropTypes/index.js containing
export default function ViewPropTypes(props) {}
var styles = StyleSheet.create({});
Still the same error message.
@franznkemaka dist/index.js:
// export const ViewPropTypes = { style: null }; // manual fix no 1
// export { default as ViewPropTypes } from './exports/ViewPropTypes'; // manual fix no 2
export { default as unstable_createElement } from './exports/createElement';
export { default as findNodeHandle } from './exports/findNodeHandle';
export { default as processColor } from './exports/processColor';
export { default as render } from './exports/render';
export { default as unmountComponentAtNode } from './exports/unmountComponentAtNode';
export { default as NativeModules } from './exports/NativeModules'; // APIs
export { default as AccessibilityInfo } from './exports/AccessibilityInfo';
export { default as Alert } from './exports/Alert';
export { default as Animated } from './exports/Animated';
export { default as Appearance } from './exports/Appearance';
export { default as AppRegistry } from './exports/AppRegistry';
export { default as AppState } from './exports/AppState';
export { default as BackHandler } from './exports/BackHandler';
export { default as Clipboard } from './exports/Clipboard';
export { default as DeviceInfo } from './exports/DeviceInfo';
export { default as Dimensions } from './exports/Dimensions';
export { default as Easing } from './exports/Easing';
export { default as I18nManager } from './exports/I18nManager';
export { default as Keyboard } from './exports/Keyboard';
export { default as InteractionManager } from './exports/InteractionManager';
export { default as LayoutAnimation } from './exports/LayoutAnimation';
export { default as Linking } from './exports/Linking';
export { default as NativeEventEmitter } from './exports/NativeEventEmitter';
export { default as PanResponder } from './exports/PanResponder';
export { default as PixelRatio } from './exports/PixelRatio';
export { default as Platform } from './exports/Platform';
export { default as Share } from './exports/Share';
export { default as StyleSheet } from './exports/StyleSheet';
export { default as UIManager } from './exports/UIManager';
export { default as Vibration } from './exports/Vibration'; // components
export { default as ActivityIndicator } from './exports/ActivityIndicator';
export { default as Button } from './exports/Button';
export { default as CheckBox } from './exports/CheckBox';
export { default as FlatList } from './exports/FlatList';
export { default as Image } from './exports/Image';
export { default as ImageBackground } from './exports/ImageBackground';
export { default as KeyboardAvoidingView } from './exports/KeyboardAvoidingView';
export { default as Modal } from './exports/Modal';
export { default as Picker } from './exports/Picker';
export { default as Pressable } from './exports/Pressable';
export { default as ProgressBar } from './exports/ProgressBar';
export { default as RefreshControl } from './exports/RefreshControl';
export { default as SafeAreaView } from './exports/SafeAreaView';
export { default as ScrollView } from './exports/ScrollView';
export { default as SectionList } from './exports/SectionList';
export { default as StatusBar } from './exports/StatusBar';
export { default as Switch } from './exports/Switch';
export { default as Text } from './exports/Text';
export { default as TextInput } from './exports/TextInput';
export { default as Touchable } from './exports/Touchable';
export { default as TouchableHighlight } from './exports/TouchableHighlight';
export { default as TouchableNativeFeedback } from './exports/TouchableNativeFeedback';
export { default as TouchableOpacity } from './exports/TouchableOpacity';
export { default as TouchableWithoutFeedback } from './exports/TouchableWithoutFeedback';
export { default as View } from './exports/View';
export { default as VirtualizedList } from './exports/VirtualizedList';
export { default as YellowBox } from './exports/YellowBox'; // compat (components)
export { default as DrawerLayoutAndroid } from './exports/DrawerLayoutAndroid';
export { default as InputAccessoryView } from './exports/InputAccessoryView';
export { default as ToastAndroid } from './exports/ToastAndroid'; // compat (apis)
export { default as PermissionsAndroid } from './exports/PermissionsAndroid';
export { default as Settings } from './exports/Settings';
export { default as Systrace } from './exports/Systrace';
export { default as TVEventHandler } from './exports/TVEventHandler'; // plugins
export { default as DeviceEventEmitter } from './exports/DeviceEventEmitter'; // hooks
export { default as useColorScheme } from './exports/useColorScheme';
export { default as useWindowDimensions } from './exports/useWindowDimensions';
I just tried out @Dammic's solution with node_modules/react-native-web/dist/exports/ViewPropTypes/index.js containing
export default function ViewPropTypes(props) {}
var styles = StyleSheet.create({});
Still the same error message.
I changed the file to only contain export default function ViewPropTypes(props) {}
and now it compiles again :) Thx everybody for your help!
@Lis-Ma you're welcome.
Your index.js looks good, actually you have two solutions, either you replace this line export { default as ViewPropTypes } from './exports/ViewPropTypes';
with export const ViewPropTypes = { style: null };
or you create a new file like you did.
In my humble opinion, it is easier to just replace the line than creating a new file with the content.
Note: each time that you will either install, remove, or update a package, your modifications will go away. So to avoid that add a script in your package.json.
So follow my comment above but instead of the code displayed there use this script instead.
Python: fix.py OS-agnostic, use this if possible for your case
import os
import sys
print("โ
Fixing PropTypes issues")
dir_path = os.path.dirname(os.path.realpath(__file__))
rnw_filename = dir_path + "/../node_modules/react-native-web/dist/index.js"
def remove_line_from_file(file_name, text_to_remove):
with open(file_name, "r+") as f:
d = f.readlines()
f.seek(0)
for i in d:
if i != text_to_remove:
f.write(i)
f.truncate()
def append_new_line(file_name, text_to_append):
"""Append given text as a new line at the end of file"""
if text_to_append in open(file_name).read():
print("โญ๏ธ Skipping...")
else:
with open(file_name, "a+") as file_object:
file_object.write("\n")
file_object.write(text_to_append)
file_object.close()
# Remove the unnecessary line first
remove_line_from_file(rnw_filename, "export { default as ViewPropTypes } from './exports/ViewPropTypes';")
# Fix
append_new_line(rnw_filename, "export const ViewPropTypes = { style: null };")
And there you go.
@Dammic you can use it as well it is easier
@Lis-Ma you're welcome.
Your index.js looks good, actually you have two solutions, either you replace this line
export { default as ViewPropTypes } from './exports/ViewPropTypes';
withexport const ViewPropTypes = { style: null };
or you create a new file like you did.In my humble opinion, it is easier to just replace the line than creating a new file with the content.
Note: each time that you will either install, remove, or update a package, your modifications will go away. So to avoid that add a script in your package.json.
So follow my comment above but instead of the code displayed there use this script instead.
Python: fix.py OS-agnostic, use this if possible for your case
import os import sys print("โ Fixing PropTypes issues") dir_path = os.path.dirname(os.path.realpath(__file__)) rnw_filename = dir_path + "/../node_modules/react-native-web/dist/index.js" def remove_line_from_file(file_name, text_to_remove): with open(file_name, "r+") as f: d = f.readlines() f.seek(0) for i in d: if i != text_to_remove: f.write(i) f.truncate() def append_new_line(file_name, text_to_append): """Append given text as a new line at the end of file""" if text_to_append in open(file_name).read(): print("โญ๏ธ Skipping...") else: with open(file_name, "a+") as file_object: file_object.write("\n") file_object.write(text_to_append) file_object.close() # Remove the unnecessary line first remove_line_from_file(rnw_filename, "export { default as ViewPropTypes } from './exports/ViewPropTypes';") # Fix append_new_line(rnw_filename, "export const ViewPropTypes = { style: null };")And there you go.
@Dammic you can use it as well it is easier
I have implemented the python fix and it gets rid of most the errors with web...howveer for a few imports I am getting this error:
TypeError: Cannot read property 'style' of undefined
static propTypes = {
65 | ...ViewPropTypes,
66 | containerStyle: ViewPropTypes.style,
67 | duration: PropTypes.number,
68 | visible: PropTypes.bool,
69 | position: PropTypes.number,
I can't figure out why it recognizes viewproptypes but not the style part
Hi @csumrell, this means that the ViewPropTypes is not defined correctly i.e undefined. This explains why you get the property 'style' of an undefined object.
What is the library that requires ViewPropTypes?
Hi @csumrell, this means that the ViewPropTypes is not defined correctly i.e undefined. This explains why you get the property 'style' of an undefined object.
What is the library that requires ViewPropTypes?
Module.../../../react-native-root-toast/lib/ToastContainer.js
node_modules/react-native-root-toast/lib/ToastContainer.js:66
@csumrell I just saw it, it looks good. Try to apply the fix manually. Does it work?
If not, please could you explain what you did exactly?
@franznkemaka, if I do nothing:
Module not found: Can't resolve 'react-native-web/dist/exports/ViewPropTypes' in '/node_modules/react-native-button'
If I use your python postinstall script, it adds "export const ViewPropTypes = { style: null };" to the bottom of index file. I then receive error:
Module not found: Can't resolve 'react-native-web/dist/exports/ViewPropTypes' in '/node_modules/react-native-button'
node_modules/react-native-button/Button.js
Then if I add the alias in my webpack:
'react-native-web/dist/exports/ViewPropTypes': 'react-native-web/dist/index.js',
I get the original style error I described TypeError: Cannot read property 'style' of undefined
Hi @csumrell,
Then the problem is that at the top of your index.js file, you have something like this:
export { default as ViewPropTypes } from './exports/ViewPropTypes';
.
Check for that line and replace it to this export const ViewPropTypes = { style: null };
.
- export { default as ViewPropTypes } from './exports/ViewPropTypes';
+ export const ViewPropTypes = { style: null };
Let me know if it works or not.
Hi @csumrell,
Then the problem is that at the top of your index.js file, you have something like this:
export { default as ViewPropTypes } from './exports/ViewPropTypes';
.
Check for that line and replace it to thisexport const ViewPropTypes = { style: null };
.- export { default as ViewPropTypes } from './exports/ViewPropTypes'; + export const ViewPropTypes = { style: null };Let me know if it works or not.
I did not have export { default as ViewPropTypes } from './exports/ViewPropTypes'; at the top of the file. However, if I add it to the file and then actually create the folder/file with only export default function ViewPropTypes(props) {} inside. it works.
But after that now i get a similar error for Text.propTypes and I am not sure how to repeat this solution for that propType
Hi @csumrell,
I got a solution for you, I tested it myself.
Go to this file node_modules\react-native-web\dist\exports\Text\index.js
Add this line
...
Text.displayName = 'Text';
+ Text.propTypes = ()=> {};
...
It will mock the styles. Now you can integrate it into the Python script too, by simply replacing the "Text.displayName = 'Text';"
with "Text.displayName = 'Text';Text.propTypes = ()=> {};"
Hope it would help.
Franz
i created script for windows but i guess it's quite easy to use it in mac os or linux.
Just add this script in postinstall section :
"scripts": { "postinstall": "shameForDevsWhoCantAddBackwardCompability.bat", ....
shameForDevsWhoCantAddBackwardCompability.bat script:
mkdir "node_modules\react-native-web\dist\exports\ViewPropTypes"; echo const ViewPropTypes = { style: null }; > node_modules\react-native-web\dist\exports\ViewPropTypes\index.js echo export default ViewPropTypes; >> node_modules\react-native-web\dist\exports\ViewPropTypes\index.js findstr "ViewPropTypes" node_modules\react-native-web\dist\index.js IF ERRORLEVEL 1 echo export { default as ViewPropTypes } from './exports/ViewPropTypes'; >> node_modules\react-native-web\dist\index.js
What it does is simply creates blank ViewPropTypes and exports it. findstr is equivalent for grep.
Hope this will help you as it helped me.
Tested with expo sdk 39 and react-native-web: 0.14.0
Hi @lubuger did you check my example? #1537 (comment)
It is easy and works for Linux, MacOS and Windows
yeah :) , i just don't want to use any external stuff (like python or e.g.)
My WORKING solution for "Module not found" from answers above tested on "expo": "^39.0.0":
- Create folder && file: ViewPropTypes/index.js in react-native-web/dist/exports
- Add below code in ViewPropTypes/index.js :
var ViewPropTypes = { style: null }
export default ViewPropTypes;
-
Import above in react-native-web/dist/index.js
export { default as ViewPropTypes } from './exports/ViewPropTypes'; -
run expo r -c
@franznkemaka Script wasn't quite working for me as it wasn't finding the exports. I modified it below and now the errors are gone. The types list can contain all the files with which you are getting errors. I was getting multiple for different dependencies.
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
types = ['ViewPropTypes', 'TextPropTypes', 'ColorPropType', 'EdgeInsetsPropType', 'PointPropType']
for type in types:
BASE_DEST = dir_path+'/node_modules/react-native-web/dist/exports/'+type+'/'
if not os.path.exists(BASE_DEST):
os.mkdir(BASE_DEST)
with open(BASE_DEST+'index.js', "w") as file_object:
file_object.write("module.exports = {}")
file_object.close()
Hi @khevamann, That's great. It means that there are many types missing. I see, thanks for sharing your code. It is easier to create folders and files as you can easily check if it doesn't exist already like you did.
My WORKING solution for "Module not found" from answers above tested on "expo": "^39.0.0":
- Create folder && file: ViewPropTypes/index.js in react-native-web/dist/exports
- Add below code in ViewPropTypes/index.js :
var ViewPropTypes = { style: null }
export default ViewPropTypes;
- Import above in react-native-web/dist/index.js
export { default as ViewPropTypes } from './exports/ViewPropTypes';- run expo r -c
@AndreiMsk your solution works for me!! Thanks for saving my day.
Building on @franznkemaka 's script (by the way thank you it fixed the issue for me), here's my version, for bash:
- in the root directory, add this as "fix-for-web.sh":
#!/bin/bash
echo 'Fixing PropTypes issues, for running expo start:web (for web)'
echo "for reference: https://github.com/necolas/react-native-web/issues/1537"
IMPORTS_REACT_NATIVE_WEB=('ViewPropTypes' 'ColorPropType' 'EdgeInsetsPropType' 'PointPropType' 'requireNativeComponent')
for import in "${IMPORTS_REACT_NATIVE_WEB[@]}"
do
echo "Fixing $import ..."
if grep -q "export const $import = { style: null };" ./node_modules/react-native-web/dist/index.js; then
echo "$import fixed already!"
else
echo -e "\nexport const $import = { style: null };">> ./node_modules/react-native-web/dist/index.js
fi
done
NOTE: depending on your OS - you might need to run: "chmod +x ./fix-for-web.sh"
- In package.json:
"scripts": { "start": "expo start", ... "postinstall": "bash ./fix-for-web.sh" },
Thanks for the solution! And everyone else as well.
This still isn't quite working for me. After running the script (which successfully adds the export lines to ./node_modules/react-native-web/dist/index.js
, I still get the following error:
Failed to compile.
./node_modules/react-native-table-component/components/rows.js
Cannot find module: 'react-native-web/dist/exports/ViewPropTypes'. Make sure this package is installed.
You can install this package by running: yarn add react-native-web/dist/exports/ViewPropTypes.
I can make the web build compile by adding the following to webpack.config.js
:
config.resolve.alias['react-native-web/dist/exports/ViewPropTypes'] = 'react-native-web/dist/index.js';
However, then I get the errors that ViewPropTypes
is null, and .style
is not defined on it.
Anyone have any ideas? Would really appreciate any help. I think it's close, and I need to get this working to get my react-native-web app to compile again.
Hi @vjsingh
Did you make sure to always run your script after npm install
or yarn install
? If not then you have to add a script.
What solution did you use exactly?
(We ended up bringing many workarounds haha)
@franznkemaka Hey,
Yeah I'm testing it before running npm install
. I understand that it has to be added into postinstall
, and have verified that the extra lines exist in ./node_modules/react-native-web/dist/index.js
when compiling.
Maybe it's because I'm running expo build
? That's the way I've been testing. I haven't found a solution yet that works, and there's quite a few affected libraries so I don't want to patch all of them individually.
@AndreiMsk 's solution looks like it may work, but I'm not sure how to put it into a script so I can run it on postinstall
However, then I get the errors that ViewPropTypes is null, and .style is not defined on it.
Then make sure to export ViewPropTypes as an object that includes style property.
So it should look like this
export const ViewPropTypes = { style: null };
or
export const ViewPropTypes = { style: ()=> null };
Yes, I used the script you provided (thanks for that!), and that exact line is at the bottom of ./node_modules/react-native-web/dist/index.js
.
Building off @AndreiMsk 's solution, I resolved this by adding the following to my fix-for-web.sh
post
script:
mkdir node_modules/react-native-web/dist/exports/ViewPropTypes
echo "var ViewPropTypes = { style: null }" >> node_modules/react-native-web/dist/exports/ViewPropTypes/index.js
echo "export default ViewPropTypes;" >> node_modules/react-native-web/dist/exports/ViewPropTypes/index.js
echo "export { default as ViewPropTypes } from './exports/ViewPropTypes';" >> node_modules/react-native-web/dist/index.js
use patch-package
first, add script at package.json
(postinstall means after effect installing)
+ "postinstall": "patch-package"
second, install patch-package
npm install --save-dev patch-package
yarn add -D patch-package postinstall-postinstall
third, open react-native-web/dist/index.js
and edit
+ export const ViewPropTypes = { style: ()=> null };
fourth, run patch-package
npx patch-package react-native-web
check created file at rootDir/patches
good luck
Hello developers, I had the same issue. Before, I manually copied the library I was using in my code, but it turns out was not the best way so I created a workaround.
Explanation If you mock ViewPropTypes in node_modules/react-native-web/dist/index.js the error will go away. So what I did is I wrote some batch file for Windows, bash for Unix-like, Python for both, it is up to you to decide which one. Just copy and paste.
Workaround
- create a .bat|.sh|.py file depending on your OS anywhere in your app and feel free to customize the mock. For me setting the style to null fixed the issue. In my case, I created a folder called scripts so my file is located at scripts/fix.bat|.sh|.py
Copy & paste the code
Python: fix.py OS-agnostic, use this if possible
import os import sys print("โ Fixing PropTypes issues") dir_path = os.path.dirname(os.path.realpath(__file__)) rnw_filename = dir_path + "/../node_modules/react-native-web/dist/index.js" def append_new_line(file_name, text_to_append): """Append given text as a new line at the end of file""" if text_to_append in open(file_name).read(): print("โญ๏ธ Skipping...") else: with open(file_name, "a+") as file_object: file_object.write("\n") file_object.write(text_to_append) file_object.close() # Fix append_new_line(rnw_filename, "export const ViewPropTypes = { style: null };")Windows Batch: fix.bat
@echo off echo 'Fixing ViewPropTypes issues' REM Fix ViewPropTypes issues ECHO export const ViewPropTypes = { style: null };>>"PATH_TO_NODE_MODULES/react-native-web/dist/index.js"Unix-like bash: fix.sh
#!/bin/bash echo 'Fixing PropTypes issues' if grep -q "export const ViewPropTypes = { style: null };" ../node_modules/react-native-web/dist/index.js; then echo "ViewPropTypes fixed already!" else echo "export const ViewPropTypes = { style: null };">> ../node_modules/react-native-web/dist/index.js fiI added in the bash a cool if-else so that you can run it infinitely without duplicating it.
Please replace PATH_TO_NODE_MODULES with the path to your node_modules folder. For example, if you at your root folder it should be "node_modules"
- (optional) Go in your package.json
Each time you run
npm install
| ``yarn install` or when you add a new package, the fix is removed, since the package refreshed. To overcome this, modify your scripts and add a post-install command that is called after. Basically, it would execute your bat script each time you hit npm or yarn install.With the python version
"postinstall": "python fix.py"
With .bat or .sh
"postinstall": "fix"
Hope it would help. It works for me. Don't hesitate to ask me anything. Thanks! Franz
Hello, Thanks for this, but I don't really understand how to go about this, any help ??
Hello, Thanks for this, but I don't really understand how to go about this, any help ??
Hi @udemezue01,
to fix the problem, you have to edit the node_modules/react-native-web/dist/index.js
file and export ViewPropTypes
so that it won't show you the error anymore.
The reason why I use a script is that each time you install new packages, npm or yarn will discard your changes. So you need a script to make sure the fix is applied after. For that yarn or npm provides you a postinstall
command inside your package.json
scripts
sections. You call call the script from there
Hope it helps
use patch-package
first, add script at package.json (postinstall means after effect installing)
+ "postinstall": "patch-package"
second, install patch-package
npm install --save-dev patch-package yarn add -D patch-package postinstall-postinstall
third, open
react-native-web/dist/index.js
and edit+ export const ViewPropTypes = { style: ()=> null };
fourth, run patch-package
npx patch-package react-native-web
check created file at rootDir/patches
good luck
thx man, it worked!
When I add the export const ViewPropTypes = { style: ()=> null };, or export const ViewPropTypes = { style: null };,
my console gives the error:
"DaysGridView.js:191 Uncaught TypeError: Cannot read properties of undefined (reading 'style')";
I have created the python script from @franznkemaka and am able to manually run it and see the expected output in react-native-web/dist/index.js but its not working when its called in the postinstall phase after running a yarn add command.
My "postinstall" looks like this:
"postinstall": "patch-package && python scripts/webfix.py"
I see the following error after yarn add [package]
/bin/sh: python: command not found
If i change my post install to use the path to python3 then its working properly:
"postinstall": "patch-package && /usr/bin/python3 scripts/webfix.py"
Does anyone know how I fix this so i can just call python rather than /usr/bin/python3?
I have created the python script from @franznkemaka and am able to manually run it and see the expected output in react-native-web/dist/index.js but its not working when its called in the postinstall phase after running a yarn add command.
My "postinstall" looks like this:
"postinstall": "patch-package && python scripts/webfix.py"
I see the following error after yarn add [package]
/bin/sh: python: command not found
If i change my post install to use the path to python3 then its working properly:
"postinstall": "patch-package && /usr/bin/python3 scripts/webfix.py"
Does anyone know how I fix this so i can just call python rather than /usr/bin/python3?
It's not related to this package at all
I have created the python script from @franznkemaka and am able to manually run it and see the expected output in react-native-web/dist/index.js but its not working when its called in the postinstall phase after running a yarn add command.
My "postinstall" looks like this:
"postinstall": "patch-package && python scripts/webfix.py"
I see the following error after yarn add [package]
/bin/sh: python: command not found
If i change my post install to use the path to python3 then its working properly:
"postinstall": "patch-package && /usr/bin/python3 scripts/webfix.py"
Does anyone know how I fix this so i can just call python rather than /usr/bin/python3?
It simply states that /usr/nin/python3 does not exist in your computer.
Run which python
to see where your Python is located and modify that.
Honestly I recommend using patch-package instead of a custom Python script