导入aws-sdk(或使用webpack构建它)时遇到问题
我的代码是
var config = require('./config')
var AWS = require('aws-sdk');
AWS.config.region = config.region;
这会导致以下构建错误:
错误 ./node_modules/aws-sdk/apis/cognito-identity-2014-06-30.min.json 模块解析失败:JSON位置0出现意外的令牌m,而 在'module.exports = {“ v ...”附近解析 加载程序来处理此文件类型。
很多次。
如果我使用代码
var config = require('./config')
var AWS = require('aws-sdk/dist/aws-sdk');
AWS.config.region = config.region;
我收到控制台错误
未捕获的TypeError:无法设置未定义的属性“ region”
我的webpack文件是:
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'
}
]
}
};
我的package.json是:
{
"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"
}
}
答案 0 :(得分:4)
当前,Webpack尚有一个未解决的问题:https://github.com/webpack/webpack/issues/7082
将type: 'javascript/auto'
添加到json文件的Webpack配置中可暂时解决该问题:
{
type: 'javascript/auto',
test: /\.json$/,
use: 'json-loader'
},