Using an npm package in a cli written in rust

Hi fellow rustacians,

I need to run some js code from an npm library in a CLI written in rust. Unsure about which way should I go.

Have the following solutions in mind though not satisfied with either of these ( might not even be correct )

  • compiling the whole application to a wasm binary and then running that. But don't want users to install wasm runtime specially for this.
  • compiling the rust code into wasm and using same in a node js project. But with this I think I would have to handle the CLI argument handling in node js which also I don't want to do and the CLI will then again have a dependency on node being present in the system. ( this still feels like a better solution than first one )

Is there a way to create a binary for this usecase which isn't dependent either on node.js or wasm runtime being present ? If not, any recommendations on how should I go about doing this ?

Any help is appreciated.

Thanks in advance : )

I don't think it's possible (or a reasonable expectation) to run NPM packages without Node. You really can't get around having the actual runtime for the language if it requires a runtime (which JavaScript does).

Wasm probably wouldn't help here. It would still require a JS runtime for running the JS code itself.

You should probably look into embedding deno into your Rust code, which is a Node.JS replacement written in Rust.

(or a reasonable expectation) to run NPM packages without Node.

I just wanted a better experience for the user. Fair point though we would ideally need some JS runtime to evaluate it.

Thanks for the recommendation on deno. With a first glace, it looks like it might work for my use case. Will update here on the findings :wink:

What's the npm package in question? Maybe we can help you find a pure Rust equivalent.

It's postman-code-generator @jeanas . As it's very coupled to postman, didn't think there will be a pure rust alternative to this. Am planning to offer the same functionality in the CLI ( pocc ) that am working on. I have successfully converted a postman request to a reqwest::request. So if there is rust crate which can convert a reqwest::Request to other languages, should be able to use it.

PS: Initial plan was to just implement conversion to curl ( which is implemented today ) though then I discovered the above library and thought it might be a great feature to add.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.