9

I have a full stack javascript application running on React, Node, express. The problem is export without default is not working in node, However, It is working fine in react. Both my node and react share the same babel configs and packages.

I've already tried adding the @babel/plugin-proposal-export-default-from package in my babel.config.js but after doing so, it generates another error

Unexpected token (25:16) export getMember;

I even tried removing the semicolumn

Here's my babel.config.js

    module.exports = {
      "presets": ['@babel/preset-env', '@babel/preset-react'],
      "plugins": [
        [
          "@babel/plugin-proposal-class-properties",
          {
            "loose": true
          }
        ]
      ]

};

Here's my package.json

{
  "name": "dpapi",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon --exec babel-node server.js --ignore dist/",
    "dev": "webpack -wd"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-eslint": "^10.0.1",
    "babel-preset-es2015": "^6.24.1",
    "eslint": "^5.16.0",
    "eslint-plugin-react": "^7.13.0",
    "nodemon": "^1.18.11",
    "webpack-cli": "^3.3.1"
  },
  "dependencies": {
    "@babel/core": "^7.4.4",
    "@babel/node": "^7.2.2",
    "@babel/plugin-proposal-class-properties": "^7.4.4",
    "@babel/plugin-proposal-export-default-from": "^7.2.0",
    "@babel/preset-env": "^7.4.4",
    "@babel/preset-react": "^7.0.0",
    "@material-ui/core": "^3.9.3",
    "@material-ui/icons": "^3.0.2",
    "axios": "^0.18.0",
    "babel-loader": "^8.0.5",
    "body-parser": "^1.19.0",
    "dropbox": "^4.0.17",
    "ejs": "^2.6.1",
    "express": "^4.16.4",
    "mongoose": "^5.5.7",
    "morgan": "^1.9.1",
    "node-fetch": "^2.5.0",
    "query-string": "^6.5.0",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-redux": "^7.0.3",
    "react-router-dom": "^5.0.0",
    "redux": "^4.0.1",
    "redux-logger": "^3.0.6",
    "webpack": "^4.30.0"
  }
}

Here's my code where the error is occuring

import dbx from '../dropbox';

const getMember = async (req, res, next) => {

    try{


        res.status(201).json({
            message: "Account created"
        });



    } catch(error){
        console.log(error)
        res.status(500).json({
            error: error
        });
    }

}

export getMember;
Waeez
  • 329
  • 1
  • 6
  • 23
  • Can be fixed by using this babel plugin https://babeljs.io/docs/en/next/babel-plugin-syntax-export-default-from.html – utkarsh Apr 16 '20 at 16:54

1 Answers1

5

You should use export default getMember or export { getMember } or export const getMember = async....

See: https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export

Tameem Safi
  • 620
  • 5
  • 6
  • only `export default getMember` is working, but I don't want this to be a default export – Waeez May 18 '19 at 13:06
  • You cannot use export the way you have used it. If you don't want it to be a default then you have to use the other way of exporting it with named exports `export { getMember }` and then when importing you will have to use the `import { getMember } from './path/to/file'` syntax. – Tameem Safi May 18 '19 at 15:43
  • Yes, I tried it this way. but this didn't work either – Waeez May 18 '19 at 17:10
  • What error is it giving you? Does it give the same error? – Tameem Safi May 18 '19 at 19:25
  • I think maybe the problem is that you need to transpile the nodejs code, since `export` only works with es6 and node does not support es modules by default. Alternatively you can try this package: https://www.npmjs.com/package/esm. – Tameem Safi May 18 '19 at 19:27
  • Does this answer the question? – Joel Day Apr 27 '20 at 19:54