{"id":21658852,"url":"https://github.com/EddyVerbruggen/nativescript-nodeify","last_synced_at":"2025-07-17T21:31:51.647Z","repository":{"id":57308666,"uuid":"77481565","full_name":"EddyVerbruggen/nativescript-nodeify","owner":"EddyVerbruggen","description":"Makes most npm packages compatible with NativeScript","archived":false,"fork":false,"pushed_at":"2018-09-04T10:27:14.000Z","size":1994,"stargazers_count":93,"open_issues_count":42,"forks_count":27,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-11-21T23:43:51.063Z","etag":null,"topics":["nativescript","node"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EddyVerbruggen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-27T20:43:51.000Z","updated_at":"2024-05-30T15:44:30.000Z","dependencies_parsed_at":"2022-09-07T03:10:28.120Z","dependency_job_id":null,"html_url":"https://github.com/EddyVerbruggen/nativescript-nodeify","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EddyVerbruggen%2Fnativescript-nodeify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EddyVerbruggen%2Fnativescript-nodeify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EddyVerbruggen%2Fnativescript-nodeify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EddyVerbruggen%2Fnativescript-nodeify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EddyVerbruggen","download_url":"https://codeload.github.com/EddyVerbruggen/nativescript-nodeify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226305036,"owners_count":17603731,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nativescript","node"],"created_at":"2024-11-25T09:29:56.805Z","updated_at":"2024-11-25T09:30:15.587Z","avatar_url":"https://github.com/EddyVerbruggen.png","language":"JavaScript","readme":"# NativeScript Nodeify\nMakes most npm packages compatible with NativeScript\n\n## Q \u0026 A\n#### Q. WTF?\nA. Good question, glad you asked! You can't just use any npm package with NativeScript as they may depend\non built-in Node modules (`fs`, `path`, `crypto` to name but a few).\nThose modules aren't plain old JavaScript files so they can't be executed in the {N} runtimes.\n\n#### Q. So how does this plugin overcome that situation?\nA. You can install dependencies as normal, and this plugin as well, then at build time a hook installed by this plugin will scan and modify\nyour npm modules ([in the platforms folder](https://github.com/EddyVerbruggen/nativescript-nodeify/pull/31)) as it sees fit to make them {N}-compatible.\n\n#### Q. Lol. Wut? Modify my precious modules!?\nA. Yes. The hook looks at the packages installed in `\u003capp\u003e/platforms/\u003cios|android\u003e/.../tns_modules` and for each dependency it does a few things:\n* Look for a `browser` node in their `package.json` and find-replaces any matching `require()` calls in that package and its dependencies.\n* If there's a `main` replacement in the `browser` node it also takes care of that.\n* If any of the dependencies (and this can go deeeeeeeeeeep - remember `left-pad`?) contains something we need to shim, we will based on [this list](https://github.com/EddyVerbruggen/nativescript-nodeify/blob/master/shims.json).\n* There's more trickery and there may be more needed, so this list is a bit evolving atm..\n\n#### Q. But doesn't browserify / Webpack solve this for us?\nA. Not in this case, at least not without further modifications. Think modules that don't have a `browser` node in their `package.json`, or modules that do but shim their node dependency with something that needs a real browser..\nFeel free to submit a PR for a nicer implementation, but this is the best I could think of.\n\n#### Q. Not bad actually, but doesn't come with a performance hit?\nA. Thanks. And good question. Most importantly, at runtime this should make no difference as you're not 'requiring'\nmore that you were already, just different implementations (that actually work, I hope).\nA build time you will see a few seconds added to your build (but it shouldn't affect livesync).\nThe hook skips checking `tns-core-modules` and anything starting with `nativescript`.\n\n## Installation\nFrom the command prompt go to your app's root folder and execute:\n\n```sh\ntns plugin add nativescript-nodeify\n```\n## Usage\nInclude this in your code before requiring the problematic npm module.\n\n```js\nrequire(\"nativescript-nodeify\");\n```\n\n## Demo app\n[The demo](https://github.com/EddyVerbruggen/nativescript-nodeify) tests a few popular\nlibraries that depend on Node built-in modules which would normally not work in a NativeScript runtime environment.\n\nRun the demo app from the root of the project: `npm run demo.ios` or `npm run demo.android`.\n\n## Know issues\nThis plugin isn't perfect, but it tried to solve issues for as many of the gazillion npm packages out there. A few issues are known, if yours is not in this list please [create an issue](https://github.com/EddyVerbruggen/nativescript-nodeify/issues/new).\n\n* Like Browserify we're using shims to fill the gaps between Node and the browser, but unlike Browserify shims we're not running in a browser, so some API's may not be available. Anything that touches the DOM for instance.  \n* The http shim isn't perfect (like [usage of `global.location.protocol.search`](https://github.com/jhiesey/stream-http/blob/master/index.js#L17)), so may need to do what [RN did](https://github.com/tradle/react-native-http) and roll our own (if anyone needs it).\n\n## Recipies\nTo get you started with a few popular npm modules, here's some recipies. Please share your own by sending a PR to this repo! \n\nAll recipies assume you've already done:\n\n```bash\n$ tns create awssdk\n$ cd awssdk\n$ tns platform add ios\n$ tns platform add android\n$ tns plugin add nativescript-nodeify\n```\n\n### `node-uuid`\n```bash\n$ npm install node-uuid --save\n```\nBoom! Done.\n\n### `jsonwebtoken`\n```bash\n$ npm install jsonwebtoken --save\n```\nBoom! Done. Again.\n\n### `aws-sdk` or `amazon-cognito-identity-js` (which includes `aws-sdk`)\nThis one requires a bit more setup, but it's not too bad:\n\n\u003e Never check in your AWS keys! Bots scan public repos and will create server instances you'll get billed for.\n\nDepending on what you need:\n\n```bash\n$ npm install aws-sdk --save\n```\n\nor\n\n```bash\n$ npm install amazon-cognito-identity-js --save\n```\n\nTo parse XML returned from AWS correctly (fi. when listing S3 bucket contents) we need\nto patch an additional library because the browser shim expects an.. ehm.. browser.\n\nSo open you app's `package.json` and add this `nodeify` node to the existing `nativescript` node:\n```json\n{\n  \"nativescript\": {\n    \"nodeify\": {\n      \"package-dependencies\": {\n        \"aws-sdk\": [\n          {\n            \"xml/browser_parser\": \"xml/node_parser\",\n            \"lib/node_loader\": \"lib/browser_loader\"\n          }\n        ]\n      }\n    }\n  }\n}\n```\n\nThen in your code for `amazon-cognito-identity-js`:\n```js\n// require this to fix an issue with xhr event states\nrequire('nativescript-nodeify');\n\n// register a user (here's a bit, but see the demo and https://github.com/aws/amazon-cognito-identity-js for details)\nvar AmazonCognitoIdentity = require('amazon-cognito-identity-js');\nvar CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool;\nvar userPool = new CognitoUserPool({UserPoolId: 'foo', ClientId: 'bar'});\n```\n\nNow just use AWS as usual:\n```js\n// then require AWS and interact with s3, dynamo, whatnot\nvar AWS = require('aws');\n```\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEddyVerbruggen%2Fnativescript-nodeify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FEddyVerbruggen%2Fnativescript-nodeify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEddyVerbruggen%2Fnativescript-nodeify/lists"}