-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NPM dependencies #111
Comments
Yeah our docs don't cover it, could be improved. I've created an example project to clarify things: Example.UsingNpmModules. To use the example
In general, for you own projects, you'll want to create a folder in your project for your package.json and call npm install manually. Then you'll want to configure MSBuild to copy the folder to your output directory on build. In the example I named the folder Javascript, and configured MSBuild to copy it by adding the following to the .csproj: <ItemGroup>
<None Include="./Javascript/**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup> In your logic you'll have to set For quick reference, here's the example project's static async Task Main()
{
// Create INodeJSService
var serviceCollection = new ServiceCollection();
serviceCollection.AddNodeJS();
serviceCollection.Configure<NodeJSProcessOptions>(options => options.ProjectPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Javascript")); // AppDomain.CurrentDomain.BaseDirectory is your bin/<configuration>/<targetframework> directory
var serviceProvider = serviceCollection.BuildServiceProvider();
var nodeJSService = serviceProvider.GetRequiredService<INodeJSService>();
// Invoke from file
string result1 = await nodeJSService.InvokeFromFileAsync<string>("./interop.js", args: new[] { "testString" }).ConfigureAwait(false);
Console.WriteLine(result1);
// Invoke from string
string interopModuleString = @"const lodash = require('lodash');
module.exports = (callback, arg) => {
callback(null, lodash.snakeCase(arg));
}";
string result2 = await nodeJSService.InvokeFromStringAsync<string>(interopModuleString, args: new[] { "testString" }).ConfigureAwait(false);
Console.WriteLine(result2);
// Expected output:
//
// test-string
// test_string
} interop.js: const lodash = require('lodash');
module.exports = (callback, arg) => {
callback(null, lodash.kebabCase(arg));
} Once you get all that working, you can consider automating package restoration. You might also want to bundle your js. You can do both those things with Yarn.MSBuild. It's a bit involved, if you're interested let me know I'll add it to the example. |
Thanks man, I think that's enough to get me going. Appreciate it. |
Hi,
Couldn't find any documentation (or maybe I missed it) on how to use it when using npm dependencies in the module you're executing. Found a few entries here, in the issues, but I'd like to know things like: does it use a packages.json, if present on disk and who runs npm install?
Also, nice lib btw.
Thanks
The text was updated successfully, but these errors were encountered: