最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to run Node JS code from npm inside of Swift - Stack Overflow

programmeradmin0浏览0评论

I am making a Cocoa App & want to call Node JS package downloaded from npm to run from Swift.

I don't know if it is possible but I couldn't find anything on the internet. Can I call an npm package from Swift?

Although it will require Node to be shipped or to be on the puter of the user running the app, I don't mind it. But can this be done?

Or do I have to write the same Node code in Swift?

I am making a Cocoa App & want to call Node JS package downloaded from npm to run from Swift.

I don't know if it is possible but I couldn't find anything on the internet. Can I call an npm package from Swift?

Although it will require Node to be shipped or to be on the puter of the user running the app, I don't mind it. But can this be done?

Or do I have to write the same Node code in Swift?

Share Improve this question asked Sep 14, 2018 at 6:23 deadcoder0904deadcoder0904 8,73318 gold badges86 silver badges208 bronze badges 2
  • Can you provide more information about what you want to do? I didn't get why you wanna run node in swift, if you want to run javascript you can do it like described here stackoverflow./questions/37434560/… – Gregory Commented Sep 22, 2018 at 3:49
  • There are some amazing modules in Node which don't exist in Swift or have to be pletely re-written. Now what if I want to make a Cocoa App using github./mixn/carbon-now-cli which acts like carbon.now.sh in a Desktop App. I can do this thing with Electron but there is a huge bloat that es with it so I want to do it with Swift :) – deadcoder0904 Commented Sep 23, 2018 at 3:37
Add a ment  | 

3 Answers 3

Reset to default 7 +50

That's a great question. I will try to give you an overview of how these different systems work and a theoretical, but rather painful solution. Swift runs on the mobile end and manages its resources on a more low level approach.

Javascript on the other hand is a general purpose language that requires a Virtual Machine(VM) that "understands" javascript, to be embedded into the system you are targeting. Thankfully, iOS has already done that with javascriptcore interface. With it, you can load up a Javascript bundle and evaluate any function from it.

In theory, what you are suggesting would work like this:

  1. Find a bundled version of the package you want to run

  2. Load it up on the javascript VM using the JavaScriptCore interface

  3. Evaluate the function of the package

Check out this great guide

The great caveat of this approach is the platform specific calls of the npm bundle (for example Node.js' fs package does not exist).

If that is the case then you should use a remote service that runs the code for your as cenk suggested.

What you can do is you can create a socket or web app via node & npm on a server, which can municate via a rest url.

Than by making request to that url, you can access it.

But of course this does not mean that you can embed npm package into your swift code. It means you can use it as a service.

I don't know how swift works, but if you can include a js file, you can just include the file. to make the file look like a simple javascript file. not nodejs file. you can use browserify it will generate single js file for the browser from node modules. there is this thing: https://cocoapods/pods/SwiftJavascriptBridge or even more https://google./?q=Swift+javascript

this article looks good: http://www.dbotha./2014/11/19/using-javascript-core-for-game-scripting-on-ios/

发布评论

评论列表(0)

  1. 暂无评论