Can we use nodejs code inside our own native application?

I want to use node js as a backend in a response native project.

+1
react-native
Nov 16 '16 at 10:33
source share
2 answers

Yes, you can use packages written for Node using ReactNativify as Big Rich . Some things to consider:

1) I followed the recommendations that I found in the list of problems and divided transformer.js into 2 parts:

transformers.js (in /config and called from rn-cli.config.js ):

 const babelTransformer = require('./babel-transformer'); module.exports.transform = function(src, filename, options) { const extension = String(filename.slice(filename.lastIndexOf('.'))); let result; try { result = babelTransformer(src, filename); } catch (e) { throw new Error(e); return; } return { ast: result.ast, code: result.code, map: result.map, filename }; }; 

babel-transformer.js (also in /config ):

 'use strict' const babel = require('babel-core'); /** * This is your `.babelrc` equivalent. */ const babelRC = { presets: ['react-native'], plugins: [ // The following plugin will rewrite imports. Reimplementations of node // libraries such as `assert`, `buffer`, etc. will be picked up // automatically by the React Native packager. All other built-in node // libraries get rewritten to their browserify counterpart. [require('babel-plugin-rewrite-require'), { aliases: { constants: 'constants-browserify', crypto: 'react-native-crypto', dns: 'mock/dns', domain: 'domain-browser', fs: 'mock/empty', http: 'stream-http', https: 'https-browserify', net: 'mock/net', os: 'os-browserify/browser', path: 'path-browserify', pbkdf2: 'react-native-pbkdf2-shim', process: 'process/browser', querystring: 'querystring-es3', stream: 'stream-browserify', _stream_duplex: 'readable-stream/duplex', _stream_passthrough: 'readable-stream/passthrough', _stream_readable: 'readable-stream/readable', _stream_transform: 'readable-stream/transform', _stream_writable: 'readable-stream/writable', sys: 'util', timers: 'timers-browserify', tls: 'mock/tls', tty: 'tty-browserify', vm: 'vm-browserify', zlib: 'browserify-zlib' }, throwForNonStringLiteral: true }], // Instead of the above you could also do the rewriting like this: ["module-resolver", { "alias": { "mock": "./config/mock", "sodium-universal": "libsodium" } }] ] }; module.exports = (src, filename) => { const babelConfig = Object.assign({}, babelRC, { filename, sourceFileName: filename }); const result = babel.transform(src, babelConfig); return { ast: result.ast, code: result.code, map: result.map, filename }; } 

2) As you can see in the above code, I also demonstrated the use of babel-plugin-module-resolver .

Note I will use this plugin instead of the one used by ReactNative. This allows you to link to local files and write using the correct quotes. It allows non-JS-compatible names such as "sodium-universal"

.babelrc Or select a .babelrc solution (possibly the cleanest) as described in this comment: https://github.com/philikon/ReactNativify/issues/4#issuecomment-312136794

3) I found that I still need .babelrc at the root of my project for my Jest tests to work. See this question for details: https://github.com/philikon/ReactNativify/issues/8

+1
Jul 20 '17 at 6:50
source share

ReactNativify The Github project does / does * exactly this, adds NodeJS runtime to React-Native (RN) projects.

* Currently does not work in RN v0.43.3 onwards, April 2017 .

+1
May 10 '17 at 2:51 am
source share



All Articles