-
Notifications
You must be signed in to change notification settings - Fork 517
Resolve node modules in parent directories #154
Comments
NodeServices never searches for Can you specify an example project layout that triggers this issue so I can repro and investigate? |
https://github.com/geirsagberg/NodeServicesFun I have basically just done the following:
Running |
Thanks for reporting this. I was surprised to hear that module resolution behaves unexpectedly because internally it just calls Node's This happens because the bootstrapping code for "webpack dev middleware" and "prerendering" is embedded into the NuGet packages as string resources, and to execute them in Node, it writes them to temporary files on disk and then gets Node to load them from there. Since the temporary directory is not within your project, when code inside those modules tries to load other modules, it has no way to find your The packages already deal with that to some extent by automatically putting a I've been trying to think of a good way to make this better, and the best option I came up with was to change how the bootstrapping modules are loaded, so that instead of just calling There might be some other way of changing how the paths are resolved. For example, there could be an optional NPM module called Altogether this is quite a tricky thing to resolve cleanly in total generality. But if you just want a way of doing it for your project, you can just set the environment variable Since there's a relatively simple solution for individual projects, and making a general built-in solution is quite complex and possibly risky, I'm going to suggest we don't try to put in a general solution but rather just document that you can set Does that seem like a reasonable solution? |
I had a hunch it might be related to the bundled resources :) Your solution seems like a good workaround, I agree that this problem doesn't warrant risky hacks. Thanks for looking into it so quickly :) |
Just a thought; could it be possible to write the temporary files to somewhere in the project folder instead? |
That would solve this immediate problem, but risks creating worse problems. We don't know that your webserver has write permission to that directory (whereas temp directories are writable), plus writing files there could trigger an app-restart loop (if you have a file watcher that restarts the app when one of its source files are written).
Thanks - I'll close this. |
How about using NuGet |
Looks like specifying NuGet |
@SteveSandersonMS why was this issue closed? The issue still exists a full year later. I want my config level items in the parent folder not in same directory as my web application. This is a common paradigm in the JavaScript community to nest your actual source code in a src subfolder but keep your webpack.config.js and node_modules in the root. I'm already having a hard enough time convincing my front end developers that we need .NET to serve their applications rather than node I don't need to make them cd into src to run npm install too... |
Thats common behavior, npm runs commands in whatever directory you're currently in.
|
Mark, I appreciate the info and that solves the problem of npm install for the developers but just like any workaround/hack it completely ignores any parameters you pass npm so then my build server has to run into the src folder to run the production version of npm install and if the time comes and this gets fixed and I want to go back and clean up my repositories then I now have to remember to fix the build server as well. As an aside I scanned the open issues on GitHub didn't see anything. I can't find any documentation on this but I tried every possible permutation of ../ ./../ .. & ./.. and nothing works node keeps executing under the context of the src folder and thus can't load any modules without a symbolic link. I spent too much time on this thinking it was user error when in reality it was just a closed issue that shouldn't have been closed. I get that it's not top priority and I appreciate all the work you guys have done to get us to this point where we can hot reload and pre-render from .NET but if issues are going to get dismissed carte blanche my confidence level drops. |
I use this workaround: https://gist.github.com/geirsagberg/0f5812d5451d2b0938b15b309c23bdd4 |
For convenience: public static class HostingEnvironmentExtensions
{
public static void UseRootNodeModules(this IHostingEnvironment hostingEnvironment)
{
var nodeDir = Path.Combine(hostingEnvironment.ContentRootPath, "../node_modules");
Environment.SetEnvironmentVariable("NODE_PATH", nodeDir);
}
} If your web project is deeper than 1 level (e.g. in a src/ folder), use Usage: public void Configure (IApplicationBuilder app, IHostingEnvironment env) {
if (env.IsDevelopment()) {
env.UseRootNodeModules();
// Extra options are just my preferences
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions {
HotModuleReplacement = true,
ReactHotModuleReplacement = true,
HotModuleReplacementClientOptions = new Dictionary<string, string> {
{"reload", "true"}
}
});
}
} |
If people are still having this issue, you can use something like this. You don't need to update the node_modules environment like above for this.
Directory structure is something like below:
|
Currently, node modules like
aspnet-webpack
must be installed in the root directory of the web app.When node modules (and package.json) are placed in a parent directory (e.g. a solution folder), node will still resolve packages correctly using
require
, but packages called via node services will fail to resolve the same packages.If NodeServices could search up the directory tree like node does, it would be easier to share node packages between multiple web projects in a single solution.
The text was updated successfully, but these errors were encountered: