Sdk: node_modules not copied to output folder when running dotnet publish

Created on 18 Aug 2016  Â·  8Comments  Â·  Source: dotnet/sdk

Steps to reproduce

  • Create a new ASP.NET Core Web App
  • Add a package.json file and specify at least one dependency
  • Add "npm install" in the scripts/prepublish section of project.json
  • Add "node_modules" in the publishOptions/include section of project.json
  • Run dotnet publish
  • See that node_modules is _not_ included in the output

I've created a GitHub repo with a project showing this.

https://github.com/henningst/NodeModulePublishRepro

  • Clone the repo above
  • Run dotnet publish without opening it in Visual Studio first (because VS is automatically installing npm packages).

Expected behavior

node_modules should be copied to the output directory when running dotnet publish because:

  • npm install runs using the prepublish hook
  • node_modules is specified to be included

It should be possible to include the node_modules directory or any other file or directory in the publish output, even if these files or directories does not exist before running scripts using the prepublish hook.

Actual behavior

When running dotnet publish on a webapp with dependencies on node packages, the node_modules directory is not copied to the output directory even if npm install is run with the prepublish hook and node_modules is specified to be included.

It works if the node_modules directory exists before running dotnet publish, but when you run dotnet publish for the first time, node_modules will not exist before npm install is run via the prepublish hook.

Try running dotnet publish a second time, and you will see that node_modules is copied.

Environment data

dotnet --info output:

.NET Command Line Tools (1.0.0-preview2-003121)

Product Information:
Version: 1.0.0-preview2-003121
Commit SHA-1 hash: 1e9d529bc5

Runtime Environment:
OS Name: Windows
OS Version: 10.0.10586
OS Platform: Windows
RID: win10-x64

Most helpful comment

@zelliott haven't looked at it in detail but I guess these packages will pull in a lot of dependencies and tracing them all down would be hard..

Any folder or file that is not yet part of the project can still be imported manually (using Include="…" instead of Update="…"):

<ItemGroup>
  <Content Include="node_modules\foo\**" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>

All 8 comments

Looks like this is a dupe of https://github.com/dotnet/cli/issues/3828

This is not a duplicate of dotnet/sdk#6609. This is a different issue. node_modules isn't empty - npm install is run as pre- publish

Any luck on this issue? I am still seeing that node_modules folder is not included as part of publish process.

I suggest using a workaround - copy what is needed using xcopy or msbuild. Certainly don't use gulp js as it's slow as hell, pipes blabla... slow as hell. Webpack is great if you are looking for a build package manager.

Following the comments on dotnet/sdk#6609 lead me to a solution for this issue as well. mkdir node_modules before calling published resulted in the node_modules directory, with all of its contents being published to the output folder

You could also remove the default node_modules exclude that the web-sdk adds by adding this to the csproj file:

  <PropertyGroup>
    <DefaultItemExcludes>$([System.String]::Copy($(DefaultItemExcludes)).Replace(';**\node_modules\**',''))</DefaultItemExcludes>
    <DefaultItemExcludes>$([System.String]::Copy($(DefaultItemExcludes)).Replace(';node_modules\**',''))</DefaultItemExcludes>
  </PropertyGroup>

However this will make building and publishing a bit slower since msbuild will scan the entire node_modules folder which may be slow.

Is there a way to edit the DefaultItemExcludes so that only a handful of the modules in node_modules are included? For example, I need aspnet-webpack and webpack-hot-middleware for the dotnet JavascriptSpa services.

@zelliott haven't looked at it in detail but I guess these packages will pull in a lot of dependencies and tracing them all down would be hard..

Any folder or file that is not yet part of the project can still be imported manually (using Include="…" instead of Update="…"):

<ItemGroup>
  <Content Include="node_modules\foo\**" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>
Was this page helpful?
0 / 5 - 0 ratings