Node.js APIs differences¶
Some Node.js APIs were designed with the purpose of running on a server/desktop environment. This means they might not work as intendend or are not supported at all.
This page contains a collection of differences for these APIs when running on a mobile environment or a description of the quirks in their behavior. Please file an issue in the core library repo for APIs that you find missing from the list.
child_process, cluster modules¶
nodejs-mobile runs in a mobile application process, which is expected to be a single process in the mobile operating systems, so forking the nodejs-mobile process results in errors for most cases. iOS doesn't give permissions to spawn new processes in most cases, as well. These modules are not supported in nodejs-mobile for the reasons presented.
console¶
The global console
instance is configured to write to process.stdout
and process.stderr
. On Android, the process.stderr
and process.stdout
streams output is sent to /dev/null
, but it can be redirected to the application logcat. The redirection is done automatically in the nodejs-mobile-cordova and nodejs-mobile-react-native plugins. Instructions on how to achieve this in Android applications running the native nodejs-mobile library can be found in the Android "Redirect Output Streams to logcat" guide.
Debugger¶
The V8 inspector is not available on current nodejs-mobile builds, due to a dependency on the intl
module.
file system module¶
The fs
module provides wrappers around standard POSIX functions, but these might not be available on the mobile OSes, file systems or work differently in a mobile application process context.
Functions that can take relative paths apply that relative path to the current working directory where the node process was started in server/desktop environments. On mobile environments, nodejs-mobile is started in the context of an application, for which the current working environment is the root of the filesystem on iOS and Android. This can lead to unexpected behavior for code that assumes the current working directory is set to the directory of the starting Node.js script (which also might not be true in a server/desktop environment).
fs.link(), fs.linkSync()¶
Android doesn't support hard links.
intl module¶
The intl
(internationalization) module is not available on current nodejs-mobile builds.
os module¶
The os
module provides operating system-related utility methods. Some of these methods are not fully supported on mobile operating systems or behave differently in the context of an application process.
os.cpus()¶
On iOS, CPU speed values are always 0, due to lack of permissions to read these values.
On Android, returns undefined since Android 8.0, due to lack of permissions to read these values. On earlier Android versions the CPU values can be inconsistent, since some devices can turn CPU cores on and off as an energy saving strategy. Properties can be returned as zero for cores that have been turned off while getting them.
os.homedir()¶
On Android and iOS there isn't the concept of a user home directory. Android sets this value to /data
and iOS sets it to root of the Application's sandboxed path.
os.platform()¶
Returns 'android' or 'ios' in nodejs-mobile, depending on the platform.
os.tmpdir()¶
On iOS, returns the application's sandboxed temporary directory, equivalent to getting the NSTemporaryDirectory
value, since iOS sets the TMPDIR environment variable for the application to that value.
The Android OS is an outlier in the sense that it doesn't have the concept of a temporary directory. In the nodejs-mobile-cordova and nodejs-mobile-react-native plugins, the nodejs-mobile runtime returns the application context's CacheDir
value, which should have a similar behavior to a temporary directory for most situations (on Android, the files in the cache are kept until the system needs space, so it increases the disk foothold of the Application unless the developer deletes them manually).
process¶
The process
object provides information about the current process where Node.js is running on, which is the application process for nodejs-mobile.
process.cwd()¶
The current working directory of the process is the same as the current working directory for the mobile application, which is be the root of the operating system on Android and iOS applications (/
).
process.exit()¶
In nodejs-mobile, the process is the application, so running process.exit()
terminates the application. This is not allowed in the Apple App Store guidelines for iOS.
process.getegid(), process.geteuid(), process.getgid(), process.getgroups(), process.getuid(), process.setegid(), process.seteuid(), process.setgid(), process.setgroups(), process.setuid()¶
These functions are only available on POSIX platforms, so they are unavailable on Android.
process.stderr, process.stdout¶
On Android, the process.stderr
and process.stdout
streams output is sent to /dev/null
, but it can be redirected to the application logcat. The redirection is done automatically in the nodejs-mobile-cordova and nodejs-mobile-react-native plugins. Instructions on how to achieve this in Android applications running the native nodejs-mobile library can be found in the Android "Redirect Output Streams to logcat" guide.
process.stdin¶
nodejs-mobile is running in the context of a mobile application, so you can expect process.stdin to not be available unless there is some native code added to pipe to it.
process.platform¶
Is 'android' or 'ios' in nodejs-mobile, depending on the platform.
process.versions¶
The process.versions
object includes the 'mobile' key, containing the nodejs-mobile core library version.