microsoft/tfs-cli

"Unexpected token import" for Azure DevOps custom task

gaelian opened this issue · 2 comments

Updated with additional information)

Relatively new to NodeJS, Typescript and custom pipeline tasks, I've written an Azure DevOps pipeline task following this guide. Everything works as expected when testing locally, but when I package and upload the task, share and install it to my Azure DevOps organisation and run the task in a pipeline, I get this error:

##[error]Unhandled: Unexpected token import
##[error]D:\a\_tasks\TestTask_0ad4956d-5534-409d-9373-33c7ab472062\0.0.1\node_modules\axios\index.js:1
(function (exports, require, module, __filename, __dirname) { import axios from './lib/axios.js';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:549:28)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)
    at Module.require (module.js:504:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous>

The error comes from the axios npm package (v1.3.3) that I'm using within the custom task to make some REST calls. The axios package has a package.json that includes "type": "module", so I gather it should be treated as using ES module syntax where the import statement should work, but it isn't being treated this way.

In an effort to make things consistent, I have also tried changing my own code to using "type": "module" in my own package.json and the same import syntax. But when subsequently running my task in a pipeline I get the same error, now just pointing at my own code instead:

D:\a\_tasks\TestTask_0ad4956d-5534-409d-9373-33c7ab472062\0.0.1\index.js:10
import tl from 'azure-pipelines-task-lib/task.js';
^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:549:28)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)
    at Module.runMain (module.js:611:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:160:9)

The version of Node.js currently running on the windows-latest hosted agent is v18.14.0 which should be new enough to support "type": "module" and my own package.json file for my own code contains "type": "module". Locally, my code (also running on Node.js v18.14.0) works as expected and is treated as using ES module syntax.

Why is apparently the same version of Node.js in the pipeline seemingly not picking up on the "type": "module" in my package.json and not recognising ES module syntax?

Repository containing a task that reproduces this issue: https://github.com/gaelian/test-task

Fixed by specifying a newer version of Node.js (that supports ES module syntax) in the task.json. For example:

    "execution": {
        "Node16": {
            "target": "index.js",
            "argumentFormat": ""
        }
    }

Hi @gaelian, closing the issue as resolved. Thanks!