我有一个server.js
文件和一个main.js
文件
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 } from '../build/main'
console.log(rnd)
import express from 'express'
let app = express()
app.listen(8080)
但它会记录undefined
。我做错了什么?
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 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]);
},{}],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){
// ...
答案 0 :(得分:0)
,您应该从app/src/main.js
import { rnd } from 'main'
使用babelify编写代码,就像在您的环境中拥有所有ES2015功能一样(但最好不时检查兼容性表)