Travis-CI Uncaught SyntaxError Use of const in strict mode

by Michael Szul on

No ads, no tracking, and no data collection. Enjoy this article? Buy us a ☕.

As I've been building more and more open source libraries and repositories, I've been try to get more and more into a proper workflow, especially when it comes to continuous integration, package management, and deployment. I decided to give Travis-CI a shot, since I see a lot of open source repositories using it. It's pretty easy to get up and running, and it's highly configurable, which also means, you can run into roadblocks, and fixing those roadblocks can sometimes be time-consuming.

Running Travis-CI for my botbuilder-config repository was pretty straightforward, but then I decided to put the code in a nodejs subfolder, so I could start to think about building out .NET Core and Python versions of the library. It wasn't as simple as moving files around, but after some investigation, it turned out to be two small lines of configuration (the cd and npm install lines below).

language: node_js
      
      before_script:
        - cd ./nodejs
      
      script:
        - npm install
        - npm run postinstall
      
      after_script:
        - npm run test
      

I had to cd into the directory, but also--since Travis runs npm install in the root by default--I needed to run the npm install, otherwise the tsc -p . for TypeScript in my "postinstall" scripts would have failed. There is a cleaner way to do this, but at the time, I was just trying to jump over a hurdle to get builds passing again.

The next hurdle was that although the build was passing, the Mocha tests actually weren't running. The reason? Node.JS was throwing an error for "Uncaught SyntaxError: Use of const in strict mode" and failing to run the tests.

After some searching, it turns out that the default Node.JS in Travis-CI is an older version that doesn't support const. This means that you need to adjust the Node.JS environment. Travis-CI lets you adjust this, and for my purposes, I needed to set it to node, which uses the latest stable release.

My configuration then looked like this:

language: node_js
      node_js:
        - "node"
      
      before_script:
        - cd ./nodejs
      
      script:
        - npm install
        - npm run postinstall
      
      after_script:
        - npm run test
      

This finally got me over the test issues, and Mocha was able to successfully run the tests.

Travis-CI is a powerful tool, and I plan to eventually set up the configuration to automatically publish to NPM on GitHub tag changes, so the workflow is even smoother.