javascript - Imported constant is undefined -


i have server.js file , main.js file compiled watchify app/src/main.js --node -t babelify -o app/build/main.js & watchify app/src/server.js --node -t babelify -o app/build/server.js

main.js:

const rnd = 3  export { rnd } 

server.js:

import { rnd } '../build/main'  console.log(rnd)  import express 'express'  let app = express()  app.listen(8080) 

but logs undefined. doing wrong?

main.js build:

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new error("cannot find module '"+o+"'");throw f.code="module_not_found",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ "use strict";  object.defineproperty(exports, "__esmodule", {   value: true }); var rnd = 3;  exports.rnd = rnd;  },{}]},{},[1]); 

server.js build:

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new error("cannot find module '"+o+"'");throw f.code="module_not_found",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ "use strict";  (function e(t, n, r) {   function s(o, u) {     if (!n[o]) {       if (!t[o]) {         var = typeof require == "function" && require;if (!u && a) return a(o, !0);if (i) return i(o, !0);var f = new error("cannot find module '" + o + "'");throw f.code = "module_not_found", f;       }var l = n[o] = { exports: {} };t[o][0].call(l.exports, function (e) {         var n = t[o][1][e];return s(n ? n : e);       }, l, l.exports, e, t, n, r);     }return n[o].exports;   }var = typeof require == "function" && require;for (var o = 0; o < r.length; o++) {     s(r[o]);   }return s; })({ 1: [function (require, module, exports) {     "use strict";      object.defineproperty(exports, "__esmodule", {       value: true     });     var rnd = 3;      exports.rnd = rnd;   }, {}] }, {}, [1]);  },{}],2:[function(require,module,exports){ 'use strict';  var _main = require('../build/main');  var _express = require('express');  var _express2 = _interoprequiredefault(_express);  function _interoprequiredefault(obj) { return obj && obj.__esmodule ? obj : { default: obj }; }  console.log(_main.rnd);  var app = (0, _express2.default)();  app.listen(8080);  },{"../build/main":1,"express":20}],3:[function(require,module,exports){  // ... 

in server.js should import app/src/main.js

import { rnd } 'main' 

using babelify write code have es2015 features in environment (but it'd better check compatibility table time time)


Comments