首页 > 解决方案 > 为什么 webpack 打包时不包含 assert.rejects(UMD 库目标)

问题描述

如果您愿意,可以将此 repo 作为起点:https ://github.com/goofballLogic/webpackery

我的代码运行一个断言

const assert = require( "assert" );
assert.rejects( () => Promise.reject() );

在 node.js 中传递。

当我使用 webpack 捆绑此代码并从节点(或例如在浏览器中)运行时,它会产生以下输出。为什么?

> ./node_modules/.bin/webpack-cli --entry ./test.js --output test.bundle.js --libraryTarget umd --mode development

Hash: d30ba0b02040a574f117
Version: webpack 4.33.0
Time: 200ms
Built at: 06/12/2019 10:50:56
         Asset      Size  Chunks             Chunk Names
test.bundle.js  51.8 KiB    null  [emitted]  null
Entrypoint null = test.bundle.js
[./node_modules/webpack/buildin/global.js] (webpack)/buildin/global.js 472 bytes {null} [built]
[./test.js] 78 bytes {null} [built]
    + 6 hidden modules


> node test.js

webpack:///./test.js?:2
assert.rejects( () => Promise.reject() );
       ^

TypeError: assert.rejects is not a function
    at eval (webpack:///./test.js?:2:8)
    at Object../test.js (/Users/andrewgibson/src/webpackery/test.bundle.js:175:1)
    at __webpack_require__ (/Users/andrewgibson/src/webpackery/test.bundle.js:20:30)
    at /Users/andrewgibson/src/webpackery/test.bundle.js:84:18
    at Object.<anonymous> (/Users/andrewgibson/src/webpackery/test.bundle.js:87:10)
    at Module._compile (internal/modules/cjs/loader.js:774:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
    at Module.load (internal/modules/cjs/loader.js:641:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:837:10)

标签: javascriptnode.jswebpack

解决方案


asserts.rejects仅在 NodeJS v10.x 及更高版本中可用。


推荐阅读