相关文章推荐
温文尔雅的火龙果  ·  import cv2 # ...·  2 月前    · 
傻傻的柚子  ·  js 如何读取word文档 - ...·  1 年前    · 
孤独的足球  ·  selenium headless模式下 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Created a new project using the aurelia-cli - SystemJS bundler option.

installed htmlparser2 module from npm which has buffer.js as a dependency.

getting the following error when attempting to import htmlparser2:

bluebird.core.js:3434 Error: global is not defined
  Evaluating http://localhost:9000/buffer/index

upon inspecting vendor-bundle -> this is the line that creates the error:

Buffer.TYPED_ARRAY_SUPPORT = global.TYPED_ARRAY_SUPPORT !== undefined
  ? global.TYPED_ARRAY_SUPPORT
  : typedArraySupport()

found a similar issue with angualar-cli where the solution was to manually turn on node global

Node global is turned off. It works fine if I manually turn it on again.

The question is how to do this using the aurelia-cli? Any suggestions?

larger code snippet from vendor-bundle

define('buffer/index',['require','exports','module','base64-js','ieee754','isarray'],function (require, exports, module) {/*!
 * The buffer module from node.js, for the browser.
 * @author   Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
 * @license  MIT
/* eslint-disable no-proto */
'use strict'
var base64 = require('base64-js')
var ieee754 = require('ieee754')
var isArray = require('isarray')
exports.Buffer = Buffer
exports.SlowBuffer = SlowBuffer
exports.INSPECT_MAX_BYTES = 50
 * If `Buffer.TYPED_ARRAY_SUPPORT`:
 *   === true    Use Uint8Array implementation (fastest)
 *   === false   Use Object implementation (most compatible, even IE6)
 * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
 * Opera 11.6+, iOS 4.2+.
 * Due to various browser bugs, sometimes the Object implementation will be used even
 * when the browser supports typed arrays.
 * Note:
 *   - Firefox 4-29 lacks support for adding new properties to `Uint8Array` instances,
 *     See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438.
 *   - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function.
 *   - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of
 *     incorrect length in some situations.
 * We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they
 * get the Object implementation, which is slower but behaves correctly.
Buffer.TYPED_ARRAY_SUPPORT = global.TYPED_ARRAY_SUPPORT !== undefined
  ? global.TYPED_ARRAY_SUPPORT
  : typedArraySupport()

I believe you are using cli built-in bundler (I wrote it), not webpack.

Yes, nodejs global var global is currently not supported. Also nodejs global vars process and Buffer have similar issues.

The cli doc has a patch to support process and Buffer.

import process from 'process';
window.process = process;
import {Buffer} from 'buffer';
window.Buffer = Buffer;

You can try to add one more patch for global.

window.global = window;

Ok why cli has the issue

cli's tracing algorithm uses rjs (requirejs optimizer) parser, it's bit old, does not detect global vars (technically it does not do variable scope analysis).

I have another WIP bundler called dumber which solved the limitation with a new parser which detects global vars. It automatically patch nodejs global vars at module level based on need.

In long term, we will drop the code for cli built-in bundler, then wrap dumber and make it backward compatible.

The following modification to buffer.js would also solve the issu: Buffer.TYPED_ARRAY_SUPPORT = typeof global !== 'undefined' && global.TYPED_ARRAY_SUPPORT !== undefined ? global.TYPED_ARRAY_SUPPORT : typedArraySupport() as the typedArraySupport() does not depend on global at all. – minus one Jul 1, 2020 at 13:45 This does not work for me. I have a very similar issue, except it happens when importing aws-sdk – Christopher Chalfant Jan 14, 2021 at 0:02

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.