node throw error when use async/await syntax.but it works well with import/export syntax

Viewed 736

nodemon throw the error when use async/await syntax:

**/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:17
export default function _asyncToGenerator(fn) {
^^^^^^
SyntaxError: Unexpected token export

but it works well with import/export syntax.

package.json

{
  "scripts": {
    "dev": "nodemon --exec babel-node server/index.js",
  }
  "dependencies": {
    "@babel/polyfill": "^7.2.5",
  },
  "devDependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.2.2",
    "@babel/node": "^7.2.2",
    "@babel/preset-env": "^7.2.3",
  }
}

.babelrc

{
  "presets": [
    "@babel/preset-env"
  ]
}

asyncToGenerator.js

function asyncGeneratorStep(...) { ... }
export default function _asyncToGenerator(fn) {
  return function () {
    var self = this,
        args = arguments;
    return new Promise(function (resolve, reject) {
      var gen = fn.apply(self, args);

      function _next(value) {
        asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
      }

      function _throw(err) {
        asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
      }

      _next(undefined);
    });
  };
}

But I think it probably goes wrong because of my babel setting.

BTW, when I use typeof, it throws the same error

**/node_modules/@babel/runtime/helpers/esm/typeof.js:3
export default function _typeof(obj) {
^^^^^^
SyntaxError: Unexpected token export

update 6/12, 2020:

structure:

src (vue app)
server (express app)
  |---- src
  |---- babel.config.js
  |---- index.js
package.json
babel.config.js

In this project, I have two babel config, one is for the vue's app, and another is for the express. What I want is running these apps at the project's root path.

And in the beginning, my script about running express is

nodemon --exec babel-node server/index.js

It can run express, but it gets the wrong babel config(project/babel.config.js)

And the solution is just to point out the specific path which babel confg you want to use(project/server/babel.config.js). So the correct script to run the express is

nodemon --exec babel-node --config-file ./server/babel.config.js server/index.js",
2 Answers

answering your comment:

nodemon doesn't know about babelrc (and it shouldn't). And babel (AFAIK) doesn't allow you to select the babelrc file that you want to use. I think that you should merge your babelrc files and set the env flag when running babel-node. Like this: babeljs.io/docs/en/6.26.3/babelrc#env-option

Another option would be to make a script that renames the babelrc file each time the app is reloaded, or something like that (I don't understand why you need 2 .babelrc files)

In an answer no longer visible (probably deleted by a moderator) I read that there are more .babelrc files in the project.

From babel docs it seems that the .babelrc needs to be in the same directory of the subpackage. I suggest you to read that doc, probably you can find the solution that better fits your requirements.

Sorry for the vague answer, but due to the lack of details in your question (server/index.js file content, directories structure, etc.) I can't do better.

Related