Azure / azure-sdk-for-js

This repository is for active development of the Azure SDK for JavaScript (NodeJS & Browser). For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/javascript/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-js.
MIT License
2.09k stars 1.2k forks source link

[storage-blob] cannot import in to commonjs project #31695

Open andyryan opened 2 hours ago

andyryan commented 2 hours ago

Describe the bug

We have a Typescript project which outputs CommonJS JS. We are trying to use the storage-blob sdk but running in to import issues.

We cannot simply change our project to an ES module as this caused a myriad of other dependencies to break (and much of the current NPM landscape is still CommonJS so I would expect MS to be able to support this).

We tried to import @azure/storage-blob in a few ways:

  1. standard Typescript import ... from: gave an error that import cannot be used outside of a ES module. Somewhat expected given the typescript documentation. So on to attempt 2
  2. javascript require: create a .js file instead of .ts, use require const { BlobServiceClient } = require("@azure/storage-blob") as per javascript documentation.

This however caused the following error:

{"level":"error","message":"Express encountered an error Unexpected token 'export'","service":"api-v2","stack":"/..../node_modules/@azure/storage-blob/dist-esm/storage-blob/src/generated/src/models/index.js:9\nexport var KnownEncryptionAlgorithmType;\n^^^^^^\n\nSyntaxError: Unexpected token 'export'\n    at wrapSafe (node:internal/modules/cjs/loader:1378:20)\n    at Module._compile (node:internal/modules/cjs/loader:1428:41)\n    at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)\n    at Object.require.extensions.<computed> [as .js] (/..../node_modules/ts-node/src/index.ts:1608:43)\n    at Module.load (node:internal/modules/cjs/loader:1288:32)\n    at Function.Module._load (node:internal/modules/cjs/loader:1104:12)\n    at Module.require (node:internal/modules/cjs/loader:1311:19)\n    at require (node:internal/modules/helpers:179:18)\n    at tryToRequire (/..../node_modules/src/util/ImportUtils.ts:21:17)\n    at importOrRequireFile (/.../node_modules/src/util/ImportUtils.ts:35:25)","timestamp":"2024-11-08 15:52:13"}

To Reproduce Steps to reproduce the behavior:

  1. create a commonJS typescript project
  2. Create a .js file
  3. import storage blob with require const { BlobServiceClient } = require("@azure/storage-blob")

Expected behavior @azure/storage-blob package is successfully imported and usable within the file

github-actions[bot] commented 2 hours ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @xgithubtriage.