1

I am converting a project over from using gulp to using nodemon so that I can use ES6 with babel. I'm following a pretty simple procedure to do so, which is well described here. A quick look at my package.json:

{
  "main": "index.js",
  "scripts": {
    "start": "FORCE_COLOR=3 nodemon --exec babel-node index.js"
  },
  "dependencies": {
    "deps": "1.1.1"
  },
  "devDependencies": {
    "@babel/cli": "^7.16.0",
    "@babel/core": "^7.16.5",
    "@babel/node": "^7.16.5",
    "@babel/polyfill": "^7.12.1",
    "@babel/preset-env": "^7.16.5",
    "nodemon": "^2.0.15"
  },
  "babel": {
    "presets": [
      [
        "@babel/preset-env",
        {
          "targets": {
            "node": "current"
          }
        }
      ]
    ]
  }
}

When I run npm start, nodemon runs the app, and reruns on save, but it crashes with a syntax error:

[nodemon] starting `babel-node index.js`
[HPM] Proxy created: /auth  -> http://localhost:8081/
/myproject/node_modules/@babel/core/lib/parser/index.js:93
    throw err;
    ^

SyntaxError: Legacy octal literals are not allowed in strict mode. (38:46)
    at Parser._raise (/myproject/node_modules/@babel/parser/src/parser/error.js:147:45)
    at Parser.raiseWithData (/myproject/node_modules/@babel/parser/src/parser/error.js:142:17)
    at Parser.raise (/myproject/node_modules/@babel/parser/src/parser/error.js:91:17)
    at Parser.recordStrictModeErrors (/myproject/node_modules/@babel/parser/src/tokenizer/index.js:1444:12)
    at Parser.readNumber (/myproject/node_modules/@babel/parser/src/tokenizer/index.js:1239:12)
    at Parser.getTokenFromCode (/myproject/node_modules/@babel/parser/src/tokenizer/index.js:951:14)
    at Parser.nextToken (/myproject/node_modules/@babel/parser/src/tokenizer/index.js:307:10)
    at Parser.next (/myproject/node_modules/@babel/parser/src/tokenizer/index.js:169:10) {
  loc: Position { line: 38, column: 46 },
  pos: 933,
  code: 'BABEL_PARSE_ERROR',
  reasonCode: 'StrictOctalLiteral'
}
[nodemon] app crashed - waiting for file changes before starting...

There doesn't seem to be a stack trace to the place in my code where this error is happening. I managed to track it down with some careful understanding of the no octal error, but other errors that come up look very similar, with no stack trace to the place in my own code where the error occurs. How can I debug like that? Is there a way to configure babel to include the origin of the error from my code?

Seth Lutske
  • 9,154
  • 5
  • 29
  • 78
  • Hm, it does have a `Position { line: 38, column: 46 }`. Not sure why it doesn't have a file name - was the mistake in some dynamically constructed string? – Bergi Dec 29 '21 at 15:41
  • Its possible, but I don't think that's the issue. I found the culprit for this error (`new Date(2020, 01, 01)`), but then had another totally different syntax error, with the same time of stack trace that has no pointer at where in my original code the error arose – Seth Lutske Dec 29 '21 at 15:42
  • This is strange. On my system, between `SyntaxError:` and `Legacy octal literals`, I see the file name. The numbers at the end of the line are the line numbers within that file, which I see in your output. The actual lines from that file are also output just before the stack trace – Codebling Jan 05 '22 at 23:19
  • That is strange....and frustrating. What might cause it not to show that on my system? Node version (14.17.6)? npm version (7.23.0)? OS (mac mojave)? – Seth Lutske Jan 06 '22 at 16:25
  • Looks like it was the Node version after all - weird. I'll file a bug report. – Codebling Jan 10 '22 at 05:59

2 Answers2

2

Running babel-node with Node 16.9 seems to fix this.

With Node 12 to 16.8:

SyntaxError: Legacy octal literals are not allowed in strict mode. (1:4)
    at Parser._raise (/tmp/babel-test-3/node_modules/@babel/parser/lib/index.js:569:17)

With Node 16.9+:

SyntaxError: /tmp/babel-test-3/index2.js: Legacy octal literals are not allowed in strict mode. (1:4)

> 1 | a = 01;
    |     ^
  2 |
    at Parser._raise (/tmp/babel-test-3/node_modules/@babel/parser/lib/index.js:569:17)

No other changes were required.

A bug report has been filed.

Codebling
  • 10,764
  • 2
  • 38
  • 66
0

It could potentially be that there is something wrong with the package itself. But babel is a properly and well maintained project so it could be something on your end

The error here is that leading zeros aren't allowed. Try and remove any instance of hard-coded leading zeros and then use +number or parseInt on any numbers which the values you won't know, such as from an API, reading from a database, or receiving user input.

destroyer22719
  • 356
  • 2
  • 6
  • This doesn't answer the question. I already explained that I found the source of the octal error, but another error popped up that was harder to track down in a large codebase. Saying that there may be something wrong with the package, or something wrong on my end, does not tell me what might be wrong that I need to fix – Seth Lutske Jan 08 '22 at 15:28
  • Yes, I understand this isn't the most helpful, but OP you never told us what lead up to this moment, you never told us how and when at what changes did it go from perfectly fine to this error. – destroyer22719 Jan 08 '22 at 15:36