使用foundation-cli創建項目時,如果當前的node版本是12的話就會出現如下錯誤:
fs.js:27 const { Math, Object } = primordials; ^ ReferenceError: primordials is not defined at fs.js:27:26
此時需要將node版本切換為10,可安裝n這個npm包來實現在一台電腦上運行多個node版本.
node版本切換為10后,再運行創建項目命令,又會出現如下錯誤:
npm ERR! git rev-list -n1 4.0: fatal: ambiguous argument '4.0': unknown revision or path not in the working tree. npm ERR! git rev-list -n1 4.0: Use '--' to separate paths from revisions, like this: npm ERR! git rev-list -n1 4.0: 'git <command> [<revision>...] -- [<file>...]' npm ERR! git rev-list -n1 4.0: npm ERR! git rev-list -n1 4.0: fatal: ambiguous argument '4.0': unknown revision or path not in the working tree. npm ERR! git rev-list -n1 4.0: Use '--' to separate paths from revisions, like this: npm ERR! git rev-list -n1 4.0: 'git <command> [<revision>...] -- [<file>...]' npm ERR! git rev-list -n1 4.0: npm ERR! git clone --template=/home/dev/.npm/_git-remotes/_templates --mirror git@github.com:gulpjs/gulp-cli.git /home/dev/.npm/_git-remotes/git-github-com-gulpjs-gulp-cli-git-15c23bd3: Cloning into bare repository '/home/dev/.npm/_git-remotes/git-github-com-gulpjs-gulp-cli-git-15c23bd3'... npm ERR! git clone --template=/home/dev/.npm/_git-remotes/_templates --mirror git@github.com:gulpjs/gulp-cli.git /home/dev/.npm/_git-remotes/git-github-com-gulpjs-gulp-cli-git-15c23bd3: Permission denied (publickey). npm ERR! git clone --template=/home/dev/.npm/_git-remotes/_templates --mirror git@github.com:gulpjs/gulp-cli.git /home/dev/.npm/_git-remotes/git-github-com-gulpjs-gulp-cli-git-15c23bd3: fatal: Could not read from remote repository. npm ERR! git clone --template=/home/dev/.npm/_git-remotes/_templates --mirror git@github.com:gulpjs/gulp-cli.git /home/dev/.npm/_git-remotes/git-github-com-gulpjs-gulp-cli-git-15c23bd3: npm ERR! git clone --template=/home/dev/.npm/_git-remotes/_templates --mirror git@github.com:gulpjs/gulp-cli.git /home/dev/.npm/_git-remotes/git-github-com-gulpjs-gulp-cli-git-15c23bd3: Please make sure you have the correct access rights npm ERR! git clone --template=/home/dev/.npm/_git-remotes/_templates --mirror git@github.com:gulpjs/gulp-cli.git /home/dev/.npm/_git-remotes/git-github-com-gulpjs-gulp-cli-git-15c23bd3: and the repository exists.
這時候需要修改文件中的package.json文件devDependencies節點下的gulp屬性,修改后的gulp字段值如下:
"gulp": "git+https://github.com/gulpjs/gulp#v4.0.0",
手動輸入npm i來安裝項目文件,這時可能又會出現如下錯誤:
phantomjs-prebuilt@2.1.14 install /Users/will/work/my-project/node_modules/phantomjs-prebuilt > node install.js PhantomJS not found on PATH Downloading https://github.com/Medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-macosx.zip Saving to /var/folders/mh/2ptfthxj2qb49jscj1b0gjsm0000gn/T/phantomjs/phantomjs-2.1.1-macosx.zip Receiving... Error making request. Error: connect ETIMEDOUT 54.231.113.227:443 at Object.exports._errnoException (util.js:1018:11) at exports._exceptionWithHostPort (util.js:1041:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
這是天朝網絡的原因:它的安裝過程中要去 github 下載一個包,而 github release 文件放在亞馬遜 aws 上(被牆了)。解決方法是借助淘寶鏡像 (http://cnpmjs.org/downloadshttps://npm.taobao.org/mirrors/phantomjs/) 安裝,輸入以下命令:
PHANTOMJS_CDNURL=https://npm.taobao.org/mirrors/phantomjs npm install phantomjs-prebuilt
同樣安裝 node-sass
也會出現類似的問題,解決方法依然是淘寶鏡像:
SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass npm install node-sass
如果不想每次都輸入前面變量可以將它們寫入 ~/.npmrc
文件中😄(https://docs.npmjs.com/files/npmrc)
sass_binary_site = https://npm.taobao.org/mirrors/node-sass phantomjs_cdnurl = https://npm.taobao.org/mirrors/phantomjs registry = https://registry.npm.taobao.org
經過這么一番折騰,項目終於跑起來了。