hp>
如果使用和老师同一个版本的 import-local 是可以正常运行的,但是 import-local 做了一次升级,增加了一些判断,结合上面的回答,可以看一下这个问题 import-local调试lerna源…-慕课网 (imooc.com),升级后的源码如下:
module.exports = filename => {
const globalDir = pkgDir.sync(path.dirname(filename));
const relativePath = path.relative(globalDir, filename);
const pkg = require(path.join(globalDir, 'package.json'));
const localFile = resolveCwd.silent(path.join(pkg.name, relativePath));
const localNodeModules = path.join(process.cwd(), 'node_modules');
const filenameInLocalNodeModules = !path.relative(localNodeModules, filename).startsWith('..');
// Use `path.relative()` to detect local package installation,
// because __filename's case is inconsistent on Windows
// Can use `===` when targeting Node.js 8
// See https://github.com/nodejs/node/issues/6624
return !filenameInLocalNodeModules && localFile && path.relative(localFile, filename) !== '' && require(localFile);
};
结合上面的回答,
filenameInLocalNodeModules
总为 true。因此总是以全局的lerna来运行。