GatsbyJS在Windows命令行创建项目出错的解决方法
一 依赖缺失引起的错误
按照官方的gatsby new project
命令创建新项目时可能会遇到下面的错误:
gatsby new blog info Creating new site from git: https://github.com/gatsbyjs/gatsby-starter-default.git Cloning into 'blog'... remote: Enumerating objects: 28, done. remote: Counting objects: 100% (28/28), done. remote: Compressing objects: 100% (25/25), done. remote: Total 28 (delta 2), reused 15 (delta 2), pack-reused 0 Unpacking objects: 100% (28/28), done. success Created starter directory layout info Installing packages... > [email protected] install D:\WorkSpace\gatsby\blog\node_modules\sharp > (node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy) info sharp Using cached C:\Users\Julian\AppData\Roaming\npm-cache\_libvips\libvips-8.8.1-win32-x64.tar.gz info sharp Creating D:\WorkSpace\gatsby\blog\node_modules\sharp\build\Release info sharp Copying DLLs from D:\WorkSpace\gatsby\blog\node_modules\sharp\vendor\lib to D:\WorkSpace\gatsby\blog\node_modules\sharp\build\Release > [email protected] postinstall D:\WorkSpace\gatsby\blog\node_modules\core-js > node -e "try{require('./postinstall')}catch(e){}" Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library! The project needs your help! Please consider supporting of core-js on Open Collective or Patreon: > https://opencollective.com/core-js > https://www.patreon.com/zloirock Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -) > [email protected] postinstall D:\WorkSpace\gatsby\blog\node_modules\core-js-pure > node -e "try{require('./postinstall')}catch(e){}" > [email protected] postinstall D:\WorkSpace\gatsby\blog\node_modules\gatsby-telemetry > node src/postinstall.js || true > [email protected] postinstall D:\WorkSpace\gatsby\blog\node_modules\cwebp-bin > node lib/install.js ‼ read ECONNRESET ‼ cwebp pre-build test failed i compiling from source × Error: Command failed: C:\WINDOWS\system32\cmd.exe /s /c "./configure --disable-shared --prefix="D:\WorkSpace\gatsby\blog\node_modules\cwebp-bin\vendor" --bindir="D:\WorkSpace\gatsby\blog\node_modules\cwebp-bin\vendor"" '.' is not recognized as an internal or external command, operable program or batch file. at D:\WorkSpace\gatsby\blog\node_modules\bin-build\node_modules\execa\index.js:231:11 at runMicrotasks (<anonymous>) at processTicksAndRejections (internal/process/task_queues.js:97:5) at async Promise.all (index 0) > [email protected] postinstall D:\WorkSpace\gatsby\blog\node_modules\mozjpeg > node lib/install.js ‼ read ECONNRESET ‼ mozjpeg pre-build test failed i compiling from source × Error: Command failed: C:\WINDOWS\system32\cmd.exe /s /c "autoreconf -fiv" 'autoreconf' is not recognized as an internal or external command, operable program or batch file. at D:\WorkSpace\gatsby\blog\node_modules\bin-build\node_modules\execa\index.js:231:11 at runMicrotasks (<anonymous>) at processTicksAndRejections (internal/process/task_queues.js:97:5) at async Promise.all (index 0) > [email protected] postinstall D:\WorkSpace\gatsby\blog\node_modules\pngquant-bin > node lib/install.js ‼ read ECONNRESET ‼ pngquant pre-build test failed i compiling from source × Error: pngquant failed to build, make sure that libpng-dev is installed at D:\WorkSpace\gatsby\blog\node_modules\bin-build\node_modules\execa\index.js:231:11 at processTicksAndRejections (internal/process/task_queues.js:97:5) at async Promise.all (index 0) npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"}) npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\chokidar\node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"}) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! [email protected] postinstall: `node lib/install.js` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the [email protected] postinstall 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\Julian\AppData\Roaming\npm-cache\_logs\2020-02-29T00_31_52_634Z-debug.log ERROR Command failed with exit code 1: npm install Error: Command failed with exit code 1: npm install - error.js:56 makeError [npm]/[gatsby-cli]/[execa]/lib/error.js:56:11 - index.js:114 handlePromise [npm]/[gatsby-cli]/[execa]/index.js:114:26 - task_queues.js:97 processTicksAndRejections internal/process/task_queues.js:97:5
解决方法
找到对应的类库逐一安装即可
C:\Users\Julian>npm install --global cwebp-bin C:\Users\Julian\AppData\Roaming\npm\cwebp -> C:\Users\Julian\AppData\Roaming\npm\node_modules\cwebp-bin\cli.js > [email protected] postinstall C:\Users\Julian\AppData\Roaming\npm\node_modules\cwebp-bin > node lib/install.js √ cwebp pre-build test passed successfully + [email protected] added 213 packages from 73 contributors in 36.105s C:\Users\Julian>npm install --global mozjpeg C:\Users\Julian\AppData\Roaming\npm\mozjpeg -> C:\Users\Julian\AppData\Roaming\npm\node_modules\mozjpeg\cli.js > [email protected] postinstall C:\Users\Julian\AppData\Roaming\npm\node_modules\mozjpeg > node lib/install.js √ mozjpeg pre-build test passed successfully + [email protected] added 213 packages from 72 contributors in 26.926s C:\Users\Julian>npm install --global pngquant-bin C:\Users\Julian\AppData\Roaming\npm\pngquant -> C:\Users\Julian\AppData\Roaming\npm\node_modules\pngquant-bin\cli.js > [email protected] postinstall C:\Users\Julian\AppData\Roaming\npm\node_modules\pngquant-bin > node lib/install.js √ pngquant pre-build test passed successfully + [email protected] added 218 packages from 72 contributors in 32.053s
安装完成后用gatsby重新创建项目即可,如果遇到编译错误可以忽视,不影响项目正常。
info Your new Gatsby site has been successfully bootstrapped. Start developing it by running: cd blog gatsby develop
二 Windows安全策略导致的出错
在PowerShell中安装完GatsbyJS后新建项目出现错误:
gatsby new my-blazing-fast-site gatsby : 无法加载文件 C:\Users\Julien\AppData\Roaming\npm\gatsby.ps1。未对文件 C:\Users\Julien\AppData\Roaming\npm\gats by.ps1 进行数字签名。无法在当前系统上运行该脚本。有关运行脚本和设置执行策略的详细信息,请参阅 https:/go.microsoft.com/f wlink/?LinkID=135170 中的 about_Execution_Policies。 所在位置 行:1 字符: 1 + gatsby new my-blazing-fast-site + ~~~~~~ + CategoryInfo : SecurityError: (:) [],PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess
这是由于Windows的安全策略导致的
解决方法
运行Set-ExecutionPolicy Unrestricted
,然后选择Y或者A
Set-ExecutionPolicy Unrestricted 执行策略更改 执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies 帮助主题所述。是否要更改执行策略? [Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 暂停(S) [?] 帮助 (默认值为“N”): Y
最后再运行gatsby new my-blazing-fast-site
来创建项目。