2

I am working on a project that uses node, where a lot of modules are in turn dependant on modules that use ECMAScript 6 code. For example, some modules use let, some use => for function definitions, etc.

The problem I'm getting is that whenever I try to build the project, I keep getting errors at exactly these locations.

The build command I'm using is:

cross-env NODE_ENV=production webpack --progress --hide-modules

And some of the errors being thrown are as follows:

ERROR in build.js from UglifyJs
Unexpected token: name (index) [./node_modules/debug/src/browser.js:155,0][build.js:1961,5]
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! qr-client@1.0.0 build-production: `cross-env NODE_ENV=production webpack --progress --hide-modules`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the qr-client@1.0.0 build-production script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

But, this error of Unexpected token: name (index) can be anything. In this case, it's because the script contains the line let index = 0, which node does not seem to understand (if I manually edit the browser.js script to use var anywhere it used to use let, it does suddenly work, but throw different errors.

To try to fix it, I tried running npm install es6, which seems to install just fine. The errors do still persist though. My npm version gives:

{
  'qr-client': '1.0.0',
  npm: '6.13.4',
  ares: '1.15.0',
  brotli: '1.0.7',
  cldr: '35.1',
  http_parser: '2.8.0',
  icu: '64.2',
  llhttp: '2.0.1',
  modules: '72',
  napi: '5',
  nghttp2: '1.40.0',
  node: '12.14.1',
  openssl: '1.1.1d',
  tz: '2019c',
  unicode: '12.1',
  uv: '1.33.1',
  v8: '7.7.299.13-node.16',
  zlib: '1.2.11'
}

So as you can see, my npm and node are updated, so that should not be a problem. Does anyone know why this problem occurs?

Before anyone asks: yes I have tried the fixes listed at npm ERR! code ELIFECYCLE, but those did not work at all. The exact same simply happened right away after trying that.

Joeytje50
  • 17,344
  • 12
  • 56
  • 87
  • To be clear, you are trying to build browser code right? This is not node code. If so, what transpiler are you using? It looks like the error comes from Uglify.js which does not seem to understand the `let` keyword. (also to be clear, node.js supports the let keyword just fine). I don't use uglify.js so you will have to read the docs on uglify.js to see if it supports es6. If not you may have to transpile it before passing to uglify.js using something like babel. – slebetman Jan 27 '20 at 02:17
  • Which node version are U using ? Are you using flag for experimental modules (es6 modules) ??? – SkorpEN Jan 27 '20 at 04:18
  • @slebetman How do I know what transpiler I have in this project? I did not initially set it all up so I wouldn't know that. I did find that indeed UglifyJS does not support ES6, so I tried using the harmony branch (https://www.npmjs.com/package/uglify-js-es6 at the bottom), but despite that (and another `npm install`), it did not seem to work still. Do you perhaps know how to check the version of a module I've got in a specific project? – Joeytje50 Jan 27 '20 at 10:36
  • @SkorpEN As shown in my post under the `npm version` output, I'm using Node 12.14.1. About the flag for experimental modules, how would I enable that? `npm run build --experimental-modules`? Or change the `build` script to also include `--experimental-modules` as well? Because both of those tries did not seem to work for me (the second one gives the error that's an invalid flag). – Joeytje50 Jan 27 '20 at 10:42
  • Does this project build correctly on development or any other node_env ???? – SkorpEN Jan 27 '20 at 10:49
  • @SkorpEN `cross-env NODE_ENV=development webpack --progress --hide-modules` does work and generate a working output for developing. – Joeytje50 Jan 27 '20 at 11:03
  • How your module.exports looks like ??? Did U tried --verbose flag during build ??? – SkorpEN Jan 27 '20 at 11:15
  • 1
    @SkorpEN I'm not sure what you mean with that. For now I have simply removed the whole UglifyJS module from the project, so the project does run now. I think I'll just be looking for another minifier instead, because this is way too much hassle. – Joeytje50 Jan 27 '20 at 13:26
  • Some people got module.exports in their package.json . And it sometimes produce error. --verbose flag should provide more detailed error message. – SkorpEN Jan 28 '20 at 09:55
  • @Joeytje50 did you ever figure this out? I mean other than removing UglifyJS? – Farasi78 Apr 30 '20 at 21:12
  • @Farasi78 Unfortunately no, we simply removed uglifyjs from the project to fix these issues. If anyone ever finds a solution, I hope they find this question to add an answer to it. – Joeytje50 Jun 03 '20 at 14:14
  • 1
    @Joeytje50 I came across this as I was upgrading Node, which caused the issue. Ultimately, my solution was probably less than optimal, because regardless of what I did some dependencies were just incompatible and causing every issue under the sun. In the end I upgraded to Angular 9 / Node 12. Started with a clean seed project and brought in all my code and just plugged through all the code changes until 0 errors & it worked. On the upside, I'm now using the 'latest and greatest' until next week when no doubt a new version will come out ;-) – Farasi78 Jun 04 '20 at 13:03

0 Answers0