npm 编译失败:Can't resolve 'child_process'

Failed to compile.

./node_modules/[email protected]@fsevents/node_modules/detect-libc/lib/detect-libc.js
Module not found: Error: Can't resolve 'child_process' in 'D:\Today\WebFront\node_modules\[email protected]@fsevents\node_modules\detect-libc\lib'

报错信息如上:一直运行好好的,突然编译报错,各种尝试提示的 npm install --save child_process fs ,也尝试删除node_modules文件夹重新npm install ,甚至把代码undo回去,重新贴回有变动的文件,未果!

百度看到有人碰见类似问题,在webpack.base.config.js中添加如下节点:

node: {

        // prevent webpack from injecting useless setImmediate polyfill because Vue
        // source contains it (although only uses it if it's native).
        //setImmediate: false,
        // prevent webpack from injecting mocks to Node native modules
        // that does not make sense for the client
        //dgram: 'empty',
       fs: 'empty',
        //net: 'empty',
        //tls: 'empty',
        child_process: 'empty'
    }

此操作会导致编译不报错,但各种警告,npm run dev 不会加载vue组件;看来这个问题还是绕不过。

 仔细想想报错前代码变动了什么:发现了下面了一项诡异import

在我添加debugger时,可能误操作vs工具自动帮我引入了这一项,果断干掉,重新npm run dev

看到了久违的绿色提示:

哎,折腾了半天时间,找问题的过程中也学习不少,vue 的运用还是很多坑,稍有不慎导致各种奇怪的错。记录于此!

发布了92 篇原创文章 · 获赞 55 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/elie_yang/article/details/102523773