设置NODE_PATH时ES2015编译失败

时间:2016-04-03 22:30:59

标签: node.js gulp babeljs

我正在尝试将node.js应用程序启动并运行到我可以的位置:

我已经弄清楚了第一部分,但出于某种原因,当我更新NODE_PATH环境变量时,一切都变成了废话。我一直试图调试这个约一天,似乎无法找到正确的解决方案。

如果有人可以帮我指出正确的方向,我会完全欣赏它。谢谢!

Github回购:https://github.com/alvincrespo/failing-es2015-compilation

终端输出

➜  app git:(master) ✗ npm start

> app@1.0.0 start /path/to/app
> NODE_PATH=./src gulp

[15:20:29] Requiring external module babel-register
[15:20:29] Using gulpfile ~/workspace/app/gulpfile.babel.js
[15:20:29] Starting 'default'...
[15:20:29] Starting 'server'...
[15:20:29] Finished 'server' after 496 μs
[15:20:29] Starting 'build'...
[15:20:29] Starting 'clean'...
[15:20:29] Finished 'clean' after 5 ms
[15:20:29] Starting 'babel'...
[15:20:29] Finished 'babel' after 269 ms
[15:20:29] Starting 'copy'...
[15:20:29] Finished 'copy' after 1.67 ms
[15:20:29] Finished 'build' after 277 ms
[15:20:29] Starting 'restart'...
[15:20:29] Finished 'restart' after 8.94 ms
[15:20:29] Starting 'watch'...
livereload[tiny-lr] listening on 35729 ...
/path/to/app/src/routes.js:1
(function (exports, require, module, __filename, __dirname) { import usersController from 'controllers/users';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (/path/to/app/dist/index.js:19:15)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)

1 个答案:

答案 0 :(得分:0)

看起来我不太明白如何设置NODE_PATH以允许“绝对”解析路径。我必须做的更新是将NODE_PATH设置为dist目录:

的package.json

  "scripts": {
    "start": "NODE_PATH=./dist gulp",
    "test": "NODE_PATH=./dist gulp test"
  },