Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

JavaScript ES6/CommonJS 構文

Focus mode
JavaScript ES6/CommonJS 構文 - AWS SDK for JavaScript
This page has not been translated into your language. Request translation

AWS SDK for JavaScript V3 API リファレンスガイドでは、 AWS SDK for JavaScript バージョン3 (V3) のすべての API オペレーションについて詳しく説明します。

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS SDK for JavaScript V3 API リファレンスガイドでは、 AWS SDK for JavaScript バージョン3 (V3) のすべての API オペレーションについて詳しく説明します。

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS SDK for JavaScript コード例は ECMAScript 6 (ES6) で記述されています。ES6 は、コードをよりモダンで読みやすくし、より多くのことをおこなうための新しい構文と新機能を提供します。

ES6 では Node.js バージョン 13.x 以降を使用する必要があります。Node.js の最新バージョンをダウンロードしてインストールするには、Node.js downloads. を参照してください。ただし、必要に応じて、次のガイドラインを使用して CommonJS 構文に例を変換することができます。

  • プロジェクト環境の package.json から "type" : "module" を削除します。

  • すべての ES6 import ステートメントを CommonJS require ステートメントに変換してください。例えば、変換します、

    import { CreateBucketCommand } from "@aws-sdk/client-s3"; import { s3 } from "./libs/s3Client.js";

    CommonJS に相当する:

    const { CreateBucketCommand } = require("@aws-sdk/client-s3"); const { s3 } = require("./libs/s3Client.js");
  • すべての ES6 export ステートメントを CommonJS module.exports ステートメントに変換してください。例えば、変換します、

    export {s3}

    CommonJS に相当する:

    module.exports = {s3}

次の例は、ES6 と CommonJS の両方で Amazon S3 バケットを作成するためのコード例を示しています。

ES6

libs/s3Client.js

// Create service client module using ES6 syntax. import { S3Client } from "@aws-sdk/client-s3"; // Set the AWS region const REGION = "eu-west-1"; //e.g. "us-east-1" // Create Amazon S3 service object. const s3 = new S3Client({ region: REGION }); // Export 's3' constant. export {s3};

s3_createbucket.js

// Get service clients module and commands using ES6 syntax. import { CreateBucketCommand } from "@aws-sdk/client-s3"; import { s3 } from "./libs/s3Client.js"; // Get service clients module and commands using CommonJS syntax. // const { CreateBucketCommand } = require("@aws-sdk/client-s3"); // const { s3 } = require("./libs/s3Client.js"); // Set the bucket parameters const bucketParams = { Bucket: "BUCKET_NAME" }; // Create the Amazon S3 bucket. const run = async () => { try { const data = await s3.send(new CreateBucketCommand(bucketParams)); console.log("Success", data.Location); return data; } catch (err) { console.log("Error", err); } }; run();
CommonJS

libs/s3Client.js

// Create service client module using CommonJS syntax. const { S3Client } = require("@aws-sdk/client-s3"); // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create Amazon S3 service object. const s3 = new S3Client({ region: REGION }); // Export 's3' constant. module.exports ={s3};

s3_createbucket.js

// Get service clients module and commands using CommonJS syntax. const { CreateBucketCommand } = require("@aws-sdk/client-s3"); const { s3 } = require("./libs/s3Client.js"); // Set the bucket parameters const bucketParams = { Bucket: "BUCKET_NAME" }; // Create the Amazon S3 bucket. const run = async () => { try { const data = await s3.send(new CreateBucketCommand(bucketParams)); console.log("Success", data.Location); return data; } catch (err) { console.log("Error", err); } }; run();

libs/s3Client.js

// Create service client module using ES6 syntax. import { S3Client } from "@aws-sdk/client-s3"; // Set the AWS region const REGION = "eu-west-1"; //e.g. "us-east-1" // Create Amazon S3 service object. const s3 = new S3Client({ region: REGION }); // Export 's3' constant. export {s3};

s3_createbucket.js

// Get service clients module and commands using ES6 syntax. import { CreateBucketCommand } from "@aws-sdk/client-s3"; import { s3 } from "./libs/s3Client.js"; // Get service clients module and commands using CommonJS syntax. // const { CreateBucketCommand } = require("@aws-sdk/client-s3"); // const { s3 } = require("./libs/s3Client.js"); // Set the bucket parameters const bucketParams = { Bucket: "BUCKET_NAME" }; // Create the Amazon S3 bucket. const run = async () => { try { const data = await s3.send(new CreateBucketCommand(bucketParams)); console.log("Success", data.Location); return data; } catch (err) { console.log("Error", err); } }; run();
PrivacySite termsCookie preferences
© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved.