[英]Command not found for react-native and create-react-app(Mac OS)
[英]Execute a shell command from react native app for mac os
我正在尝试构建一个基于 react native( https://github.com/ptmt/react-native-macos )的 mac os 应用程序,该应用程序执行 shell 脚本并在应用程序上显示 Z78E6221F6393D1356D6681。 我想从运行一个简单的ls
命令开始。
文档 - https://nodejs.org/api/child_process.html#child_process_spawning_bat_and_cmd_files_on_windows
以下是我的代码
import React from 'react';
import {
Button,
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
View,
} from 'react-native';
import {Colors} from 'react-native/Libraries/NewAppScreen';
const {spawn} = require('child_process');
class App extends React.Component {
super;
constructor(props) {
super(props);
this.state = {
textValue: 'Change me',
};
}
onPress = () => {
const ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', data => {
console.log(`stdout: ${data}`);
this.setState({
textValue: data,
});
});
};
render() {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={styles.scrollView}>
<View style={{paddingTop: 25}}>
<Text>{this.state.textValue}</Text>
<Button title="Change Text" onPress={this.onPress} />
</View>
</ScrollView>
</SafeAreaView>
</>
);
}
}
const styles = StyleSheet.create({
scrollView: {
backgroundColor: Colors.lighter,
},
body: {
backgroundColor: Colors.white,
},
});
export default App;
但是抛出错误。
error: Error: Unable to resolve module child_process from /Users/Ramu/apps/PdfTools/App.js: child_process could not be found within the project.
If you are sure the module exists, try these steps:
1. Clear watchman watches: watchman watch-del-all
2. Delete node_modules and run yarn install
3. Reset Metro's cache: yarn start --reset-cache
4. Remove the cache: rm -rf /tmp/metro-*
10 | } from 'react-native';
11 | import {Colors} from 'react-native/Libraries/NewAppScreen';
> 12 | const {spawn} = require('child_process');
| ^
我希望 child_process 应该是 react native bundle 的一部分。 可以请一些机构帮助解决它。
这似乎是一个旧帖子,但我会添加一个答案以防其他人阅读。 似乎 child_process nodejs 模块不适用于 React Native。 然而,有一个 React 特定的子进程 package: https://www.npmjs.com/package/react-native-childprocess
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.