【问题日志】全局安装webpack,终端运行webpack指令出现错误

webpack指令

$ webpack
/Users/Super/node_modules/webpack-cli/bin/cli.js:244
                throw err;
                ^
Error: Cannot find module 'webpack'
    at Function.Module._resolveFilename (module.js:555:15)
    at Function.Module._load (module.js:482:25)
    at Module.require (module.js:604:17)
    at require (/Users/Super/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
    at Object. (/Users/Super/node_modules/webpack-cli/bin/convert-argv.js:7:24)
    at Module._compile (/Users/Super/node_modules/v8-compile-cache/v8-compile-cache.js:178:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Module.require (module.js:604:17)
    at require (internal/module.js:11:18)
    at yargs.parse (/Users/Super/node_modules/webpack-cli/bin/cli.js:241:14)
    at Object.parse (/Users/Super/node_modules/yargs/yargs.js:563:18)
    at /Users/Super/node_modules/webpack-cli/bin/cli.js:219:8
    at Object. (/Users/Super/node_modules/webpack-cli/bin/cli.js:538:3)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Module.require (module.js:604:17)
    at require (internal/module.js:11:18)
    at module.exports.filename (/usr/local/lib/node_modules/webpack-cli/node_modules/import-local/index.js:16:66)
    at /usr/local/lib/node_modules/webpack-cli/bin/cli.js:13:6
    at Object. (/usr/local/lib/node_modules/webpack-cli/bin/cli.js:538:3)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)

解决方法

npm link webpack


原因分析

在我运行这条指令之后,终端出现如下

/Users/Super/node_modules/webpack -> /usr/local/lib/node_modules/webpack

猜想由于全局安装。本地运行的webpack,指向全局安装路径所在webpack目录。这时我的 /Users/Super/node_modules/下多了个webpack文件夹替身


参考文章

Error: Cannot find module ‘webpack’

last update time 2022-02-26