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"
}
}
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'
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With