dragonwong / blog

a blog based on github page
dragonwong.github.io/blog/
9 stars 4 forks source link

Jesus! Npm make me go mad! #17

Open dragonwong opened 6 years ago

dragonwong commented 6 years ago

本来是在开发 rn 的打包工具 metro,开发环境折腾了半天,link 之后还要 build,很麻烦,发 issue 求教了(How to develop Metro in an actual RN project?)。

先不管麻不麻烦了,能跑起来总是好的,结果一跑报错,来自于我的 metro 开发目录脚本 /{mypath}/WS/github/metro/packages/metro/src/node-haste/lib/toLocalPath.js

'use strict';
var _require = require('path');
const relative = _require.relative,
  basename = _require.basename;

// FIXME: This function has the shortcoming of potentially returning identical
// paths for two files in different roots.
function toLocalPath(roots, absolutePath) {
  for (let i = 0; i < roots.length; i++) {
    const localPath = relative(roots[i], absolutePath);
    if (localPath[0] !== '.' || basename(absolutePath) == localPath) {
      return localPath;
    }
  }

  console.log(roots, absolutePath);

  throw new Error(
    `Expected path \`${absolutePath}\` to be relative to one of the project roots`,
  );
}

module.exports = toLocalPath;

报错内容如下:

[ '/{mypath}/WS/test/rn/UnbundleTestProject2', '/usr/local/lib/node_modules/metro' ]
'/{mypath}/WS/github/metro/packages/metro/src/lib/bundle-modules/HMRClient.js'

Expected path `/{mypath}/WS/github/metro/packages/metro/src/lib/bundle-modules/HMRClient.js` to be relative to one of the project roots

估计是 link 之后目录校验没对上。这都没事。我想看看之前都是什么样的,于是我就 unlink,结果发现不好使(实际项目目录和本地 metro 开发目录都 unlink 了),然后就去实际项目目录删除 node_module 重新 install,发现安装不上了,再试 npm cache cleannpm cache verify 统统不好使。

报错依次如下:

npm unlink metro

⋊> ~/W/t/r/UnbundleTestProject2 npm unlink metro                                                                                                                                                                                                                  15:06:41
npm WARN checkPermissions Missing write access to /{mypath}/WS/test/rn/UnbundleTestProject2/node_modules/metro
npm ERR! path /{mypath}/WS/test/rn/UnbundleTestProject2/node_modules/metro
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall access
npm ERR! enoent ENOENT: no such file or directory, access '/{mypath}/WS/test/rn/UnbundleTestProject2/node_modules/metro'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent

npm ERR! A complete log of this run can be found in:
npm ERR!     /{mypath}/.npm/_logs/2018-04-14T07_07_00_115Z-debug.log

npm install metro@0.28.0

npm install metro@0.28.0                                                                                                                                                                                                          15:08:09
npm WARN checkPermissions Missing write access to /{mypath}/WS/test/rn/UnbundleTestProject2/node_modules/metro
npm WARN @babel/plugin-check-constants@7.0.0-beta.38 requires a peer of @babel/core@7.0.0-beta.38 but none is installed. You must install peer dependencies yourself.

npm ERR! path /{mypath}/WS/test/rn/UnbundleTestProject2/node_modules/metro
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall access
npm ERR! enoent ENOENT: no such file or directory, access '/{mypath}/WS/test/rn/UnbundleTestProject2/node_modules/metro'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent

npm ERR! A complete log of this run can be found in:
npm ERR!     /{mypath}/.npm/_logs/2018-04-14T07_08_34_759Z-debug.log

疯了。


在高玩指导之前,我还是先跑起来吧!无奈新建了一个实际项目,npm install link 啥的都毫无压力。当然刚开始目录没对上的错还是有的,好解,改下 /{mypath}/WS/github/metro/packages/metro/src/node-haste/lib/toLocalPath.js

'use strict';
var _require = require('path');
const relative = _require.relative,
  basename = _require.basename;

// FIXME: This function has the shortcoming of potentially returning identical
// paths for two files in different roots.
function toLocalPath(roots, absolutePath) {
  for (let i = 0; i < roots.length; i++) {
    if (roots[i] === '/usr/local/lib/node_modules/metro') {
      roots[i] = '/{mypath}/WS/github/metro';
    }

    const localPath = relative(roots[i], absolutePath);
    if (localPath[0] !== '.' || basename(absolutePath) == localPath) {
      return localPath;
    }
  }

  throw new Error(
    `Expected path \`${absolutePath}\` to be relative to one of the project roots`,
  );
}

module.exports = toLocalPath;

这样,就先跑起来了。