Closed cosmoKenney closed 2 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):
webpack.config.js
:
https://github.com/JeringTech/Javascript.NodeJS/blob/8aaab655438ed3436a829a05f859432ad49afb0f/src/NodeJS/Javascript/webpack.config.js#L1-L26package.json
:
https://github.com/JeringTech/Javascript.NodeJS/blob/8aaab655438ed3436a829a05f859432ad49afb0f/src/NodeJS/Javascript/package.json#L3-L5Yarn.MSBuild
to your .Net project:
https://github.com/JeringTech/Javascript.NodeJS/blob/8aaab655438ed3436a829a05f859432ad49afb0f/src/NodeJS/Jering.Javascript.NodeJS.csproj#L57-L59You 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.
@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.
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:
the bundle is embedded in the dll:
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.
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 thenode_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 theNodeJSProcessOptions.ProjectPath
?