Skip to content
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

Cannot find module 'aspnet-webpack' #5169

Closed
RaleS opened this issue Aug 20, 2017 · 18 comments
Closed

Cannot find module 'aspnet-webpack' #5169

RaleS opened this issue Aug 20, 2017 · 18 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-spa

Comments

@RaleS
Copy link

RaleS commented Aug 20, 2017

Exception thrown when starting the app created by using Service Fabric Asp.Net Core 2.0 Angular template. Running on Service Fabric 5.7.198.

"Webpack dev middleware failed because of an error while loading 'aspnet-webpack'. Error was: Error: Cannot find module 'aspnet-webpack'\n at Function.Module._resolveFilename (module.js:485:15)\n at Function.Module._load (module.js:437:25)\n at Module.require (module.js:513:17)\n at require (internal/module.js:11:18)\n at Object. (C:\SfDevCluster\Data\_App\_Node_2\Application4Type_App59\temp\nlcuhlos.kzt:83:19)\n at webpack_require (C:\SfDevCluster\Data\_App\_Node_2\Application4Type_App59\temp\nlcuhlos.kzt:20:30)\n at createWebpackDevServer (C:\SfDevCluster\Data\_App\_Node_2\Application4Type_App59\temp\nlcuhlos.kzt:62:26)\n at C:\SfDevCluster\Data\_App\_Node_2\Application4Type_App59\temp\vyr4pqpo.yvi:114:19\n at IncomingMessage. (C:\SfDevCluster\Data\_App\_Node_2\Application4Type_App59\temp\vyr4pqpo.yvi:133:38)\n at emitNone (events.js:105:13)\nCurrent directory is: C:\SfDevCluster\Data\_App\_Node_2\Application4Type_App59\Web1Pkg.Code.1.0.0\r\n"

@RaleS
Copy link
Author

RaleS commented Aug 23, 2017

The app loads if the Service Fabric app package includes node.exe, node_modules, Client_App and webpack.config.js. But it seems that Webpack dev middleware does not pick up / reload the file changes.

@SteveSandersonMS
Copy link
Member

It sounds like you're trying to run your app in Service Fabric without first publishing it. As in, you're deploying the files that come directly out of the template, instead of deploying the output from dotnet publish -c Release or using Visual Studio's "publish" menu option.

When running in production, make sure your ASP.NET app is set to "production" environment. Then it won't attempt to run Webpack dev middleware (which you shouldn't enable in production - it's only a dev-time feature).

@RaleS
Copy link
Author

RaleS commented Sep 8, 2017

Thanks for the reply. I think you should try the scenario and reconsider your choice.

The exception above is thrown when running on dev-machine.

To reproduce:

  1. Use the Visual Studio to create a new Service Fabric Application project. Select Stateless ASP.NET Core application. Ensure you are viewing ASP.NET Core 2.0 templates. Select Angular as project template.
  2. Since it is a Service Fabric project, you will have to add following environment variable to run in development mode:
    [Environment]::SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development", "Machine")
  3. Debug the project - expect the exception in Startup.cs ln 34.

You should either (a) fix the issue, (b) remove the HotModuleReplacement option for Angular template in Service Fabric apps, or (c) remove the Angular template option for Service Fabric apps.

Our workaround is to set the web project as the StartUp project to get the HMR to work.
We disable the HMR when running in local Service Fabric cluster.

@SteveSandersonMS
Copy link
Member

Ah, I see, thanks for clarifying. I thought you were talking about a production app, not in development. Reopening for investigation.

@zelliott
Copy link

zelliott commented Sep 13, 2017

Running into a similar issue. Do apps running in Service Fabric even support WebpackDevMiddleware and HotModuleReplacement?

@WillTartak
Copy link

WillTartak commented Sep 27, 2017

I'm running into the same issue. Is there a known workaround? I'm running 6.0.211/2.8.211

@WillTartak
Copy link

WillTartak commented Sep 27, 2017

To move forward for now we commented out this in Startup.Configure:

app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
{
	HotModuleReplacement = false
});

@JasonBro
Copy link

I've just bumped into this issue when deploying to a new AWS server. I published the app to my local file system then copied it over to the AWS server - and got a generic .net Core error message saying something along the lines of 'Computer says no. Set ASPNETCORE_ENVIRONMENT to Development for more information...'

So I duly set that environment variable - found out that Node.js wasn't installed; installed that - then bumped into this error.

After a bit of head-scratching, it became pretty obvious that the reason I was getting this error is because I set ASPNETCORE_ENVIRONMENT to Development so I could find out more information about errors! I followed WillTartak's advice above and it all worked fine - it also all worked fine when I deleted the ASPNETCORE_ENVIRONMENT environment variable.

So, fundamentally, if you're running on something that doesn't support HotModuleReplacement, then either make sure that ASPNETCORE_ENVIRONMENT isn't set to Development or only allow HotModuleReplacement in DEBUG, e.g.

#if DEBUG
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
{
	HotModuleReplacement = true
});
#endif

@Igorbek
Copy link

Igorbek commented Nov 5, 2017

I've got to the same issue, however it may have caused by different reason.
So I started to get this error after I tried to re-layout npm packages with yarn workspaces.
That made my aspnet-webpack package instance moved upper in folder hierarchy, since now it is a shared package across the workspaces.
I briefly explored the source code to find out why it could be happening. I expected to see any explicit assumptions that this module (aspnet-webpack) must be in the current's folder node_modules, but there was none. The module is require-d from embedded resource by just its name, so node should've just resolved it.
Interestingly, after manually copying that package to application folder's node_module, it started to work, and even used other modules that were in different locations (standard node resolution).
I suspect that, somehow, loading modules from dynamic javascript content (came from embedded resource) is only supported from local node_modules. That is super inconvenient to me, since prevents using yarn workspaces.

@Igorbek
Copy link

Igorbek commented Nov 5, 2017

For the reference, here's my error message:

Unhandled Exception: System.AggregateException: One or more errors occurred.
---> Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException: Webpack dev middleware failed because of an error while loading 'aspnet-webpack'. Error was: Error: Cannot find module 'aspnet-webpack'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (<temp>\0effiqcn.zgq:83:19)
    at __webpack_require__ (<temp>\0effiqcn.zgq:20:30)
    at createWebpackDevServer (<temp>\0effiqcn.zgq:62:26)
    at <temp>\xq21zjmq.0rz:114:19
    at IncomingMessage.<anonymous> (<temp>\xq21zjmq.0rz:133:38)
    at emitNone (events.js:86:13)
Current directory is: <web app folder>

   at Microsoft.AspNetCore.NodeServices.HostingModels.HttpNodeInstance.<InvokeExportAsync>d__7`1.MoveNext()

Note, current directory was correctly set to the application's directory. So node should have resolved the module by going upper, where aspnet-webpack was living (../../node_modules/aspnet-webpack).

@SteveSandersonMS
Copy link
Member

So node should have resolved the module by going upper

That would be convenient, but unfortunately that's not quite how Node works in general: aspnet/JavaScriptServices#154

@Igorbek
Copy link

Igorbek commented Nov 11, 2017

Thank you @SteveSandersonMS that clarified a lot. Setting NODE_PATH worked as a workaround.

@andrerocha
Copy link

Thanks JasonBro, work for me.

@Acinate
Copy link

Acinate commented Dec 13, 2017

I ran 'npm install' in the root of project directory and it fixed everything for me.

@luminescent
Copy link

I had a similar issue for the web API project with the React template, so writing it down in case someone else needs it. I ended up doing this:

            var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
            var pathToContentRoot = Path.GetDirectoryName(pathToExe)?
                .Replace(@"\bin\Debug\netcoreapp2.0", "")
                .Replace(@"\bin\Release\netcoreapp2.0", "");

            Configuration = new ConfigurationBuilder()
                .SetBasePath(pathToContentRoot)     
...

            var webHostBuilder = WebHost
                .CreateDefaultBuilder()
                .UseContentRoot(pathToContentRoot)
...

This is because in certain conditions the base path is not set to the project root but to the output folder. For the latter, it means the node package is not available, since it is installed in the project root and not in the output folder.

@CuddleBunny
Copy link

In Service Fabric I've found that I don't encounter this error when using Refresh Mode on a local single node cluster. However the webpack builds only seem to occur if wwwroot/dist does not exist (triggering the csproj target) or a clean is performed. I have a related issue here Azure/service-fabric-issues#1146.

@CuddleBunny
Copy link

Upon further investigation I think something is happening with the NodeService itself. WebpackDevMiddleware doesn't see any publicPath when running in Service Fabric. When requesting a page it will hit HttpNodeInstance.InvokeExportAsync. In SF the response is the original content of the page every time while without SF it properly reflects any changes made to files like home.component.html. Running the project in SF also yields a number of strangely named files (stuff like 1fwblf4c.rqs or 2pynt1ke.dxf) with JS webpack code in them in C:\SfDevCluster\Data\_App\_Node_0\NgTestType_App1\temp

@aspnet-hello aspnet-hello transferred this issue from aspnet/JavaScriptServices Dec 17, 2018
@aspnet-hello aspnet-hello added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-spa labels Dec 17, 2018
@mkArtakMSFT
Copy link
Member

Closing this issue as there was no community involvement for quite a while now.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-spa
Projects
None yet
Development

No branches or pull requests