I am using common js module system in my angularjs 1.x application and struggling with importing/exporting constants. This is what I have.
index.js
'use strict';
var angular = require('angular');
module.exports = angular.module('constants', [])
.constant('constant1', require('./constant1'))
.constant('constant2', require('./constant2'))
.constant('constant3', require('./constant3'))
constant1.js
'use strict';
var SAMPLE1 = { 'TEST1' : 100 , 'Test2': 200 }
var SAMPLE2 = 300;
module.exports = SAMPLE1 ;
module.exports = SAMPLE2 ;
constant2.js
'use strict';
var SAMPLE3 = { 'TEST3' : 400 , 'Test4': 500 }
var SAMPLE4 = 600;
module.exports = SAMPLE3 ;
module.exports = SAMPLE4 ;
constant3.js
'use strict';
var SAMPLE5 = { 'TEST5' : 700 , 'Test6': 800 }
var SAMPLE6 = 900;
module.exports = SAMPLE5 ;
module.exports = SAMPLE6 ;
app.module.js
'use strict';
var angular = require('angular');
var myApp = angular.module('myApp', [
require('./common/services').name,
require('./components/constants').name
]);
I am getting this error while running the application
Error: $injector:unpr
Unknown provider: KEYProvider <- SAMPLE1 <- MyService
It says you have an error in declaration of injections of your service MyService
. If you want to export multiple variables you have to write module.exports.var1 = var1; module.exports.var2 = var2
(or exports.var1 = var1; exports.var2 = var2
) and not just module.exports = var1; module.exports = var2,
because the second assignment simply overrides the first one. Later you use var1 = require('./file').var1
to import var1 and var2 = require('./file').var2
to import var2