Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't import aws-sdk using webpack, and npm installed aws-sdk

I am having an issue importing the aws-sdk (or building it with webpack)

my code is

var config = require('./config')
var AWS = require('aws-sdk');
AWS.config.region = config.region;

which causes a build error of:

ERROR in ./node_modules/aws-sdk/apis/cognito-identity-2014-06-30.min.json Module parse failed: Unexpected token m in JSON at position 0 while parsing near 'module.exports = {"v...' You may need an appropriate loader to handle this file type.

many times.

if I use the code

var config = require('./config')
var AWS = require('aws-sdk/dist/aws-sdk');
AWS.config.region = config.region;

I get a console error of

Uncaught TypeError: Cannot set property 'region' of undefined

my webpack file is:

module.exports = { 
  entry: 'entry.js',
  // Place output files in `./dist/my-app.js`
  output: {
    path: __dirname + '/dist',
    filename: 'my-app.js'
  },  
  resolve: {.
      extensions: [ '.js', '.json', '.jsx'].
  },  
  module: {
    rules: [
      {   
        test: /\.json$/,
        loader: 'json-loader'
      }   
    ]   
  }
};

and my package.json is:

{
  "private": true,
  "dependencies": {
    "amazon-cognito-identity-js": "^2.0.9",
    "aws-sdk": "^2.261.1",
    "crypto-browserify": "^3.12.0",
    "js-cookie": "^2.2.0",
    "webpack-dev-server": "^3.1.4"
  },
  "devDependencies": {
    "json-loader": "^0.5.7",
    "webpack": "^4.12.0",
    "webpack-cli": "^3.0.8"
  },
  "scripts": {
    "build": "webpack --mode development",
    "serve": "webpack-dev-server --mode development --port 3000"
  }
} 
like image 860
GreatGather Avatar asked Dec 03 '25 10:12

GreatGather


1 Answers

There is an open issue with Webpack for this currently: https://github.com/webpack/webpack/issues/7082

Add type: 'javascript/auto' to your Webpack config for json files solves it for the time being:

  {
    type: 'javascript/auto',
    test: /\.json$/,
    use: 'json-loader'
  },
like image 131
Elliot Ledger Avatar answered Dec 04 '25 23:12

Elliot Ledger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!