[英]Migrated application to Angular 6 : But getting errors while running npm run build --prod. But command npm run build --env=prod runs successfully
[英]The command "npm run build -- --prod" exited with code 1 error
我正在 Visual Studio 2017 中開發 Asp.Net Core 2 和 Angular 5 項目。
當我要發布我的項目時,錯誤列表 window 中顯示錯誤“命令 "npm run build -- --prod" exited with code 1 '。
我在 ASP.NET Core 2 Angular 模板鏈接中使用 Angular CLI創建了項目,一切都很好,項目運行和發布正確。
過了一會兒,我不知道究竟發生了什么導致項目沒有發布。
在 npm-debug.log 文件中
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 ]
只要有錯誤就執行以下任一選項
將參數傳遞給 build 命令:
將 .csproj 文件中的“npm run build -- --prod”更改為“npm run build --prod”
使用此 CMD 命令打開 ClientApp 文件夾並更新項目 angular-cli:
npm install --save-dev @angular/cli@latest
從以下位置更新 Visual Studio NodeJS 和 npm:
Tools > Options > Projects and Solutions > Web Package Management > External Web Tools
如果您的環境路徑上未安裝 NodeJS 和 npm 將 NodeJS 和 npm 文件夾添加到列表頂部,否則將 $(path) 移動到列表頂部
感謝@Praveen Kumar 和@Yerkon
遇到同樣的錯誤,我的環境:
.NET Core 2.1 RC-1
Angular v5.2.4
NPM v5.7.1
Node v8.11.2
上面提到的一切都沒有幫助。 簡單轉到 ".\\ClientApp" //Angula-CLI 項目並運行
npm run build
然后你會看到真正的錯誤——這意味着你在代碼中有一些錯誤,所以修復 angular UI 代碼,它會很好地構建:)。
運行:
npm run build:prod -- --prod
查看導致構建失敗的實際錯誤。
注意:注意該命令有兩次prod
。
檢查這個也許你有內存不足的問題
"build": "node --max-old-space-size=4069 node_modules/@angular/cli/bin/ng build --prod",
Error: The command "npm install" exited with code 1
添加環境變量,右擊此PC
C:\\Users\\User\\AppData\\Roaming\\npm
C:\\Program Files\\nodejs
如果Path
變量已經存在,那么您可以編輯該變量添加新路徑(值) ;
分離。
也許在命令行 NodeJS 版本的 Visual Studio 是舊的。 在您的命令輸出中,我們可以看到:
節點@v6.10.3
更新NodeJS
嘗試運行: ng build "prod" 並查看實際錯誤是什么。
從輸出窗口中的錯誤來看,似乎 npm build 命令針對的是錯誤的目錄,並且找不到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 ]
我懷疑您的 msbuild 是針對您的根項目文件夾,而不是您的 package.json 文件所在的ClientApp/SpaRoot文件夾。(根據類似經驗猜測,因為我無權訪問您的 .csproj 來檢查您的 msbuild 配置)。
如果您打開 .csproj 文件,您應該會看到ClientApp/SpaRoot文件所在的位置。 理想情況下,它將類似於以下內容:
<PropertyGroup>
<SpaRoot>ClientApp\</SpaRoot>
<DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
</PropertyGroup>
基於上述內容,將配置ClientApp/SpaRoot文件夾的目錄路徑,其中包含package.json文件,並將在部署期間調用npm build
時使用:
<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>
如果在netlify 上部署,請在站點設置構建和部署時使用以下命令而不是npm run build
。
CI= npm run build
我在 .csproj 文件中將 'npm run build -- --prod' 更改為 'npm run build --prod' 但發布無法正常工作,我的實時代碼變得開放,這不是真的。 我在 angular.json 文件中進行了以下更改。(最大警告和最大錯誤:我增加了大小)“預算”:[{“類型”:“初始”,“最大警告”:“8mb”,“最大錯誤”:“16mb " }, { "type": "anyComponentStyle", "maximumWarning": "8kb", "maximumError": "16kb" } ] 然后 npm 運行構建 -- --prod 工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.