51

I'm developing an Asp.Net Core 2 and Angular 5 project in visual studio 2017.

When I'm going to publish my project then the error 'The command "npm run build -- --prod" exited with code 1' show in error list window.
I created the project with Angular CLI in ASP.NET Core 2 Angular template link and everything was fine and the project run and publish correctly.
After a while, I don't know what exactly happens that cause the project doesn't publish.

In the npm-debug.log file

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'build',
1 verbose cli   '--',
1 verbose cli   '--prod' ]
2 info using npm@3.10.10
3 info using node@v6.10.3
4 verbose stack Error: ENOENT: no such file or directory, open 'C:\Project\JWS\JWSApplication\package.json'
4 verbose stack     at Error (native)
5 verbose cwd C:\Project\JWS\JWSApplication
6 error Windows_NT 10.0.15063
7 error argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build" "--" "--prod"
8 error node v6.10.3
9 error npm  v3.10.10
10 error path C:\Project\JWS\JWSApplication\package.json
11 error code ENOENT
12 error errno -4058
13 error syscall open
14 error enoent ENOENT: no such file or directory, open 'C:\Project\JWS\JWSApplication\package.json'
15 error enoent ENOENT: no such file or directory, open 'C:\Project\JWS\JWSApplication\package.json'
15 error enoent This is most likely not a problem with npm itself
15 error enoent and is related to npm not being able to find a file.
16 verbose exit [ -4058, true ]
Iman Bahrampour
  • 6,180
  • 2
  • 41
  • 64

11 Answers11

83

Perform any of the following options as long as there is an error

  1. For passing an argument to build command:

    change 'npm run build -- --prod' to 'npm run build --prod' in the .csproj file

  2. Open ClientApp folder and Update project angular-cli with this CMD command:

    npm install --save-dev @angular/cli@latest

  3. Update visual studio NodeJS and npm from:

    Tools > Options > Projects and Solutions > Web Package Management > External Web Tools

If NodeJS and npm aren't installed on your environment path Add NodeJS and npm folder to the top of the list else move $(path) to the top of the list

enter image description here.

Thank @Praveen Kumar and @Yerkon

Iman Bahrampour
  • 6,180
  • 2
  • 41
  • 64
  • 1
    I also encountered the same issue and step 1 worked for me. Thanks, this saves me much time. – Setrákus Ra Aug 16 '18 at 07:00
  • 2
    VS 2017, 15.8.1, .net core 2 Angular Template: still had to apply thix fix. Thanks! Don't get it why Microsoft won't fix it. – Bluesight Aug 30 '18 at 12:31
  • Where do you put this "npm install --save-dev @angular/cli@latest" ? i've open .angular-cli.json but i didn't find the npm install string. – Mr. Developer Dec 18 '18 at 13:37
  • @Mr.Developer. Open CMD in the clientApp folder and run the npm install... command. – Iman Bahrampour Dec 18 '18 at 15:35
  • @ImanBahrampour i've alredy done it, but on the .angular-cli.json i don't find this string npm install --save-dev @angular/cli@latest – Mr. Developer Dec 18 '18 at 16:02
  • @Mr.Developer. the 'install --save-dev @angular/cli@latest' is a cmd command and update angular CLI of a project. the angular-cli.json is about project configuration and 'install --save-dev @angular/cli@latest' command does not exist there. – Iman Bahrampour Dec 21 '18 at 05:37
  • 1
    At my end it worked when I updated to VS 2019 and changed csproj to 'npm run build --prod'. Surprisingly it did not help when I was running on VS 2017. – Marcin Janowski Jun 07 '19 at 08:50
  • **be aware**, when removing the first two `--` from `npm run build -- --prod`! The additional `--` are telling npm that anything after this **should be appended directly to the command itself** as stated here: [link](https://corgibytes.com/blog/2017/04/18/npm-tips/) – guppy81 Feb 24 '20 at 12:44
  • VS19 adding --prod at the end of the command. So if the script says npm run build --prod, it will become npm run build -- --prod – Sahib Khan Jul 29 '20 at 04:11
  • 1
    change 'npm run build -- --prod' to 'npm run build --prod' in the .csproj file : this part was enough for me. Super Thanks – Chathura Liyanage Feb 22 '22 at 18:26
14

Got same error, my environment:

.NET Core 2.1 RC-1
Angular v5.2.4
NPM v5.7.1
Node v8.11.2

And nothing mentioned above did not helped. Simple go to ".\ClientApp" //Angula-CLI project and run

npm run build

then you will see real errors - so that means simple you have some errors in code, so fix angular UI code and it will build fine :).

Drasius
  • 825
  • 1
  • 11
  • 26
  • 1
    This was my real problem, relying on VS to make build don't point really where code problems are on angular Framework that only pops up when running ng build --prod – CREM Nov 27 '18 at 17:54
  • The question is about "npm run build -- --prod" error not real errors. – Iman Bahrampour Dec 28 '18 at 17:59
  • Before insert any answer it's better to read the question carefully. The 'npm run build -- --prod' exist in the .csproj file and doesn't related to 'npm run build'. you must fix the 'npm run build -- --prod' error. – 2nyacomputer Dec 29 '18 at 20:22
14

Run:

npm run build:prod -- --prod

to see the actual error/errors that caused your build to fail.

Update: the new command is:

npm run build -- --configuration production

Fouad Boukredine
  • 1,495
  • 14
  • 18
3

Check this maybe you have problem with lack of memory

 "build": "node --max-old-space-size=4069 node_modules/@angular/cli/bin/ng build --prod",
Mohammad Hassani
  • 511
  • 6
  • 14
3

Error: The command "npm install" exited with code 1

Add environment variable, right click this PC

  • Go to Properties
  • Go to Advance tab
  • Click environment variable
  • In User variable section click new, Enter variable name "Path" and value C:\Users\User\AppData\Roaming\npm
  • One more - In User variable section click new, Enter variable name "Path" and value C:\Program Files\nodejs

If Path variable already there, then you can edit that variable add new path(value) by ; separation.

R15
  • 13,982
  • 14
  • 97
  • 173
2

if deploying on netlify, use below command instead of npm run build while in site setting build & deploy.

CI= npm run build

1

Maybe in command line NodeJS version of Visual Studio is old. In your command output we can see:

node@v6.10.3

Update NodeJS

Yerkon
  • 4,548
  • 1
  • 18
  • 30
1

Try running: ng build "prod" and see what the actual error is.

1

From the error in your output window, it seems that the npm build command is targeting the wrong directory and cannot find the package.json file to perform its build:

11 error code ENOENT
12 error errno -4058
13 error syscall open
14 error enoent ENOENT: **no such file or directory, open** 'C:\Project\JWS\JWSApplication\package.json'
15 error enoent ENOENT: no such file or directory, open 
'C:\Project\JWS\JWSApplication\package.json'
15 error enoent This is most likely not a problem with npm itself
15 error enoent and is related to npm not being able to find a file.
16 verbose exit [ -4058, true ]

I suspect your msbuild is targeting your root project folder instead of your ClientApp/SpaRoot folder where your package.json file will be located.(Guess based on similar experience since I do not have access to your .csproj to inspect your msbuild configurations).

If you open your .csproj file, you should see where the ClientApp/SpaRoot file is located. Ideally, it will be something like below:

<PropertyGroup>
        <SpaRoot>ClientApp\</SpaRoot>
        <DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
</PropertyGroup>

Based on the above, the directory path to your ClientApp/SpaRoot folder will be configured which contains the package.json file and will be used when calling npm build during deployment:

<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
        <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
        <Exec **WorkingDirectory="$(SpaRoot)"** Command="npm install" />
        <Exec **WorkingDirectory="$(SpaRoot)"** Command="npm run build --prod" />
        <Exec **WorkingDirectory="$(SpaRoot)"** Command="npm run build:ssr --prod" Condition=" '$(BuildServerSideRenderer)' == 'true' " />
        
        <!-- Include the newly-built files in the publish output -->
        <ItemGroup>
            <DistFiles Include="$(SpaRoot)dist\**; $(SpaRoot)dist-server\**" />
            <DistFiles Include="$(SpaRoot)node_modules\**" Condition="'$(BuildServerSideRenderer)' == 'true'" />
            <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
                <RelativePath>%(DistFiles.Identity)</RelativePath>
                <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
                <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
            </ResolvedFileToPublish>
        </ItemGroup>
    </Target>
MG89
  • 31
  • 3
0

i changed 'npm run build -- --prod' to 'npm run build --prod' in the .csproj file but publish did not work correctly and my codes in live became open and it wasn't true. I made the following change in the angular.json file.(maximumWarning and maximumError: I increased the size) "budgets": [ { "type": "initial", "maximumWarning": "8mb", "maximumError": "16mb" }, { "type": "anyComponentStyle", "maximumWarning": "8kb", "maximumError": "16kb" } ] and then npm run build -- --prod worked!

0

Go to angular.json file, configurations->production->budgets, increase maximumWarning and maximumError more than the error is showing.

"budgets": [
        {
          "type": "initial",
          "maximumWarning": "12mb",
          "maximumError": "12mb"
        }
      ]
nazim hatipoglu
  • 634
  • 10
  • 24