《React-Native系列》14、 RN学习之NodeJS

由于ReactNative使用了NodeJS环境,以前没有系统学习过NodeJS,故花点时间学习下。了解ReactNative里关于NodeJS部分的知识。

我们先看看一个RN项目的目录,如下:


我们看到的node_modules 和package.json都是和NodeJS相关联的。 学习完这边文章你就能明白:

1、我们在调试时,使用的包服务器,启动命令:npm start ,是什么意思?

2、我们在down一个项目下来以后,为什么要执行下:npm install ?

3、我们如果管理我们的node资源包?包括下载、更新、发布。


模块

编写稍大一点的程序时一般都会将代码模块化。在NodeJS中,一般将代码合理拆分到不同的JS文件中,每一个文件就是一个模块,而文件路径就是模块名。

在编写每个模块时,都有requireexportsmodule三个预先定义好的变量可供使用。

require

require函数用于在当前模块中加载和使用别的模块,传入一个模块名,返回一个模块导出对象。模块名可使用相对路径(以./开头),或者是绝对路径(以/C:之类的盘符开头)。另外,模块名中的.js扩展名可以省略。以下是一个例子。

var foo1 = require('./foo');
var foo2 = require('./foo.js');
var foo3 = require('/home/user/foo');
var foo4 = require('/home/user/foo.js');

// foo1至foo4中保存的是同一个模块的导出对象。

另外,可以使用以下方式加载和使用一个JSON文件,模块名中.json扩展名不可省略。

var data = require('./data.json');
exports

exports对象是当前模块的导出对象,用于导出模块公有方法和属性。别的模块通过require函数使用当前模块时得到的就是当前模块的exports对象。以下例子中导出了一个公有方法。

exports.hello = function () {
    console.log('Hello World!');
};
module

通过module对象可以访问到当前模块的一些相关信息,但最多的用途是替换当前模块的导出对象。例如模块导出对象默认是一个普通对象,如果想改成一个函数的话,可以使用以下方式。

npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: myApp2@1.0.0 npm ERR! Found: @types/react@18.3.20 npm ERR! node_modules/@types/react npm ERR! dev @types/react@"^18.0.0" from the root project npm ERR! peerOptional @types/react@"^18" from @tarojs/taro@4.0.9 npm ERR! node_modules/@tarojs/taro npm ERR! @tarojs/taro@"4.0.9" from the root project npm ERR! peer @tarojs/taro@">=3" from taro-ui@3.3.0 npm ERR! node_modules/taro-ui npm ERR! taro-ui@"^3.2.1" from the root project npm ERR! 2 more (@tarojs/components, @tarojs/taro-rn) npm ERR! npm ERR! Could not resolve dependency: npm ERR! peerOptional @types/react@"^19.0.0" from react-native@0.78.1 npm ERR! node_modules/react-native npm ERR! peer react-native@">=0.62.0" from taro-ui@3.3.0 npm ERR! node_modules/taro-ui npm ERR! taro-ui@"^3.2.1" from the root project npm ERR! peer react-native@"*" from @bam.tech/react-native-image-resizer@3.0.11 npm ERR! node_modules/@bam.tech/react-native-image-resizer npm ERR! peer @bam.tech/react-native-image-resizer@"^3.0.10" from @tarojs/taro-rn@4.0.9 npm ERR! node_modules/@tarojs/taro-rn npm ERR! peer @tarojs/taro-rn@">=3" from taro-ui@3.3.0 npm ERR! node_modules/taro-ui npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! npm ERR! For a full report see: npm ERR! C:\Program Files\nodejs\node_cache\_logs\2025-03-26T05_43_45_318Z-eresolve-report.txt npm ERR! A complete log of this run can be found in: C:\Program Files\nodejs\node_cache\_logs\2025-03-26T05_43_45_318Z-debug-0.log
最新发布
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值