0

I'm attempting to migrate a project that hooks into Azure DevOps to use ES6 module loading with Node 14.1

package.json includes field

"type": "module",

tsconfig.json includes fields

"target": "ES2020",
"module": "es2020",
"moduleResolution": "node",

Transpiled JS results in

import * as azdev from "azure-devops-node-api";
let orgUrl = process.env.API_URL;
let token = process.env.API_TOKEN;
let authHandler = azdev.getPersonalAccessTokenHandler(token);
let connection = new azdev.WebApi(orgUrl, authHandler);

Which gives the error

TypeError: azdev.getPersonalAccessTokenHandler is not a function
    at file:///Users/paul/repos/azure-devops-node-api/simple/_build/index.js:4:25
    at ModuleJob.run (internal/modules/esm/module_job.js:110:37)
    at async Loader.import (internal/modules/esm/loader.js:179:24)

I think I may be attempting to use the function before it's imported asynchronously but can't figure out how to solve this.

Paul Hale
  • 307
  • 4
  • 14

1 Answers1

0

Update your tsconfig.json file like this:

{
"compilerOptions": {
    "target": "es2020",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true
},
"files": [
    ...
]

}

Result: enter image description here

There is the sample: https://github.com/microsoft/azure-devops-node-api/blob/master/samples/tsconfig.json

starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
  • This uses commonjs for module loading. I'm attempting to use ES6 modules. – Paul Hale May 06 '20 at 08:52
  • @PaulHale Based on this thread: [TypeScript tsconfig settings for Node.js 12?](https://stackoverflow.com/questions/59787574/typescript-tsconfig-settings-for-node-js-12), it seems that you need to use commonjs module and set specify target. Are you able to use es2020 as module with the simple sample? If so, please share it. – starian chen-MSFT May 08 '20 at 07:29
  • @PaulHale Do you have any other questions? – starian chen-MSFT May 11 '20 at 08:14