JeringTech / Javascript.NodeJS

Invoke Javascript in NodeJS, from C#
Other
463 stars 43 forks source link

How to deploy a project with local node_modules? #119

Closed cosmoKenney closed 2 years ago

cosmoKenney commented 3 years ago

If I use npm to install modules within my project structure (most likely in the root of the project), then how do you recommend I deploy the project? Should I simply set NodeJSProcessOptions.ProjectPath to "./" and set the node_modules folder properties in visual studio to copy always? Or would it be better to setup a folder on the server with node modules installed there. Then set that path in the NodeJSProcessOptions.ProjectPath?

JeremyTCD commented 3 years ago

This is subjective/situational. I'd recommend the first method if you're deploying to the cloud since cloud servers are typically transient. Otherwise the second method would work too.

Either way I would also bundle the JS instead of copying the entire node_modules. You can bundle on build (code below taken from this project, situation is slightly different - bundling for a Nuget package, not for an application - so do tweak as needed):

You might know this already, the bundle output by webpack is basically a module, i.e. it exports whatever your entry JS file exported. So you can invoke from the bundle as you would from an interop.js file.

Let me know if you have any issues.

cosmoKenney commented 3 years ago

@JeremyTCD Thanks I hadn't thought about webpack. I have extensive experience with it. My initial mindset was to take the easiest route to save time. But now that I think about it this project is deployed via ci/cd so manual file copy or npm install on the target vm could get kinda ugly.

JeremyTCD commented 3 years ago

Yeah I reckon setting up bundling is quicker than writing extra deployment scripts, especially if you're already familiar with Webpack.

Just a note if you decide to bundle - for the code I quoted above:

https://github.com/JeringTech/Javascript.NodeJS/blob/8aaab655438ed3436a829a05f859432ad49afb0f/src/NodeJS/Jering.Javascript.NodeJS.csproj#L95-L98

the bundle is embedded in the dll:

https://github.com/JeringTech/Javascript.NodeJS/blob/8aaab655438ed3436a829a05f859432ad49afb0f/src/NodeJS/Jering.Javascript.NodeJS.csproj#L41-L42

This is because it's difficult to include arbitrary files in a Nuget package (hard to include a separate .js file).

In your case you can just output the bundle straight into your bin directory, no need to embed. This might reduce the complexity of your .csproj a bit.