如何解决 ENOENT:没有这样的文件或目录,在具有多个项目的工作区中使用 ngx-build-plus 打开“\package.json”?
Posted
技术标签:
【中文标题】如何解决 ENOENT:没有这样的文件或目录,在具有多个项目的工作区中使用 ngx-build-plus 打开“\\package.json”?【英文标题】:How to solve ENOENT: no such file or directory, open '\package.json' with ngx-build-plus in workspace with multiple projects?如何解决 ENOENT:没有这样的文件或目录,在具有多个项目的工作区中使用 ngx-build-plus 打开“\package.json”? 【发布时间】:2021-07-03 14:46:27 【问题描述】:在下面描述的工作区中,我无法使用 ngx-build-plus 构建没有 package.json 的项目/插件。如果我改回默认构建器,它可以正常工作。工作区具有以下结构(为清楚起见,大部分项目被省略):
- projects
- shared
- src
- lib
...
- package.json
...
- plugins
- src
- app
... plugin app is here
- webpack.config.js
- src
- app
... the main app is here
-webpack.config.js
-angular.json
-package.json
我正在尝试实现有角度的 webpack-5 模块联合,我正在遵循示例:
https://github.com/alexzuza/angular-plugin-architecture-with-module-federation
在示例中,项目/插件没有自己的 package.json,使用命令 ng build plugins --prod 构建它没有任何问题。
我已经用我以前没有项目文件夹的现有应用程序模仿了示例的结构。构建主应用程序不是问题。还构建共享库作品。但是尝试构建插件会产生:
'node-sass' usage is deprecated and will be removed in a future major version. To opt-out of the deprecated behaviour and start using 'sass' uninstall 'node-sass'.
- Generating browser application bundles...Compiling @angular/core : es2015 as esm2015
Compiling @angular/common : es2015 as esm2015
Compiling shared : es2015 as esm2015
Compiling @angular/platform-browser : es2015 as esm2015
Compiling @angular/platform-browser-dynamic : es2015 as esm2015
Compiling @angular/router : es2015 as esm2015
✔ Browser application bundle generation complete.
An unhandled exception occurred: ENOENT: no such file or directory, open '\package.json'
See "C:\Users\char-m\AppData\Local\Temp\ng-plu2me\angular-errors.log" for further details.
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file C:\WINDOWS\system32\cmd.exe
npm ERR! errno ENOENT
npm ERR! NIS@0.0.0 build:plugins: `ng build plugins --prod`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the NIS@0.0.0 build:plugins script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\char-m\AppData\Roaming\npm-cache\_logs\2021-04-07T19_34_04_884Z-debug.log
在上面的终端输出中单击“\package.json”会毫无问题地在工作区根目录中打开 package.json。
将 package.json 从根目录复制到 projects/plugins/ 解决了这个问题。但是我不想复制文件。
编辑:如果我像这样将 Angular 版本更新为旧版本(从 11.1.1 开始):
Angular CLI: 11.0.7
Node: 14.15.1
OS: win32 x64
Angular: 11.0.0
... animations, cdk, common, compiler, compiler-cli, core, forms
... material, material-moment-adapter, platform-browser
... platform-browser-dynamic, platform-server, router
Ivy Workspace: Yes
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1100.7
@angular-devkit/build-angular 0.1100.7
@angular-devkit/core 11.0.7
@angular-devkit/schematics 11.0.7
@angular/cli 11.0.7
@angular/language-service 11.2.9
@schematics/angular 11.0.7
@schematics/update 0.1100.7
ng-packagr 11.2.4
rxjs 6.5.5
typescript 4.0.7
webpack 5.31.0
构建工作。但是,这不是一个选项,因为这会产生 another bug when serving the application。
任何解决此问题的想法都将受到高度赞赏。
文件 angular-errors.log 如下所示:
[error] HookWebpackError: ENOENT: no such file or directory, open '\package.json'
at makeWebpackError (C:\dev\nis\NIS\ClientApp\node_modules\webpack\lib\HookWebpackError.js:49:9)
at C:\dev\nis\NIS\ClientApp\node_modules\webpack\lib\Compilation.js:2394:12
at eval (eval at create (C:\dev\nis\NIS\ClientApp\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:31:1)
at fn (C:\dev\nis\NIS\ClientApp\node_modules\webpack\lib\Compilation.js:376:17)
at _next4 (eval at create (C:\dev\nis\NIS\ClientApp\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:28:1)
at eval (eval at create (C:\dev\nis\NIS\ClientApp\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:47:1)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
-- inner error --
Error: ENOENT: no such file or directory, open '\package.json'
at Object.openSync (fs.js:476:3)
at Object.readFileSync (fs.js:377:35)
at CacheBackend.provideSync (C:\dev\nis\NIS\ClientApp\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:233:32)
at WebpackFileSystem.readFileAsUtf8 (C:\dev\nis\NIS\ClientApp\node_modules\license-webpack-plugin\dist\WebpackFileSystem.js:52:24)
at PluginChunkReadHandler.getPackageJson (C:\dev\nis\NIS\ClientApp\node_modules\license-webpack-plugin\dist\PluginChunkReadHandler.js:35:43)
at PluginChunkReadHandler.processModule (C:\dev\nis\NIS\ClientApp\node_modules\license-webpack-plugin\dist\PluginChunkReadHandler.js:45:40)
at C:\dev\nis\NIS\ClientApp\node_modules\license-webpack-plugin\dist\PluginChunkReadHandler.js:29:23
at WebpackModuleFileIterator.internalCallback (C:\dev\nis\NIS\ClientApp\node_modules\license-webpack-plugin\dist\WebpackModuleFileIterator.js:28:13)
at WebpackModuleFileIterator.iterateFiles (C:\dev\nis\NIS\ClientApp\node_modules\license-webpack-plugin\dist\WebpackModuleFileIterator.js:8:9)
at C:\dev\nis\NIS\ClientApp\node_modules\license-webpack-plugin\dist\PluginChunkReadHandler.js:27:32
文件 2021-04-07T19_34_04_884Z-debug.log 如下所示:
0 info it worked if it ends with ok
1 verbose cli [
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:plugins'
1 verbose cli ]
2 info using npm@6.14.8
3 info using node@v14.15.1
4 verbose run-script [ 'prebuild:plugins', 'build:plugins', 'postbuild:plugins' ]
5 info lifecycle NIS@0.0.0~prebuild:plugins: NIS@0.0.0
6 info lifecycle NIS@0.0.0~build:plugins: NIS@0.0.0
7 verbose lifecycle NIS@0.0.0~build:plugins: unsafe-perm in lifecycle true
8 verbose lifecycle NIS@0.0.0~build:plugins: PATH: C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\dev\nis\NIS\ClientApp\node_modules\.bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Program Files\nodejs\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\150\DTS\Binn\;C:\Program Files\Azure Data Studio\bin;C:\Program Files (x86)\Yarn\bin\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files\dotnet\;C:\Program Files (x86)\dotnet\;C:\Users\char-m\AppData\Local\Microsoft\WindowsApps;;C:\Users\char-m\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\char-m\AppData\Roaming\npm;C:\Program Files\Azure Data Studio\bin;C:\Users\char-m\AppData\Local\Yarn\bin;C:\Users\char-m\.dotnet\tools
9 verbose lifecycle NIS@0.0.0~build:plugins: CWD: C:\dev\nis\NIS\ClientApp
10 silly lifecycle NIS@0.0.0~build:plugins: Args: [ '/d /s /c', 'ng build plugins --prod' ]
11 info lifecycle NIS@0.0.0~build:plugins: Failed to exec build:plugins script
12 verbose stack Error: NIS@0.0.0 build:plugins: `ng build plugins --prod`
12 verbose stack spawn ENOENT
12 verbose stack at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:48:18)
12 verbose stack at ChildProcess.emit (events.js:315:20)
12 verbose stack at maybeClose (internal/child_process.js:1048:16)
12 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
13 verbose pkgid NIS@0.0.0
14 verbose cwd C:\dev\nis\NIS\ClientApp
15 verbose Windows_NT 10.0.18363
16 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build:plugins"
17 verbose node v14.15.1
18 verbose npm v6.14.8
19 error code ELIFECYCLE
20 error syscall spawn
21 error file C:\WINDOWS\system32\cmd.exe
22 error errno ENOENT
23 error NIS@0.0.0 build:plugins: `ng build plugins --prod`
23 error spawn ENOENT
24 error Failed at the NIS@0.0.0 build:plugins script.
24 error This is probably not a problem with npm. There is likely additional logging output above.
25 verbose exit [ 1, true ]
【问题讨论】:
【参考方案1】:我正在使用 NX 工作区,我也面临同样的问题。在查看 webpack 文档后,我可以设法解决这个问题。
我做了什么,我在构建命令结束时使用了 --no-extract-licenses 并且效果很好。
之前:nx build yourAppName --prod
之后:nx build yourAppName --prod --no-extract-licenses
希望这对你也有用...
【讨论】:
以上是关于如何解决 ENOENT:没有这样的文件或目录,在具有多个项目的工作区中使用 ngx-build-plus 打开“\package.json”?的主要内容,如果未能解决你的问题,请参考以下文章
npm 错误! Windows_NT 6.1.7601:ENOENT:没有这样的文件或目录
如何解决此错误:ENOENT:没有此类文件或文件或目录,在本机错误时打开“/tmp/mt-1miyjxv/os.json”?