0

I tried to deploy my project developed using React and Node.js to Heroku, but after issuing git push heroku master, I got an error:

2020-06-05T02:12:18.092681+00:00 app[web.1]: import express from "express";
2020-06-05T02:12:18.092682+00:00 app[web.1]: ^^^^^^^
2020-06-05T02:12:18.092682+00:00 app[web.1]:
2020-06-05T02:12:18.092683+00:00 app[web.1]: SyntaxError: Unexpected identifier
2020-06-05T02:12:18.092683+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:718:23)

I googled and it is said that Node.js doesn't support ES6 syntax, and I can solve it by using babel. But I don't know how to configure it.

Below are my files:

.babelrc.json:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ]
  ]
}

package.json:

{
  "name": "react_e-commerce",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon --watch backend --exec babel-node backend/server.js",
    "build": "rimraf dist && babel backend -d dist",
    "heroku-postbuild": "npm run build && cd e-commerce && npm install && npm run build"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.9.16"
  },
  "devDependencies": {
    "@babel/cli": "^7.10.1",
    "@babel/core": "^7.10.1",
    "@babel/node": "^7.10.1",
    "@babel/preset-env": "^7.10.1",
    "babel-cli": "^6.26.0",
    "mkdirp": "^1.0.4",
    "nodemon": "^2.0.4",
    "rimraf": "^3.0.2"
  },
  "engines": {
    "node": "12.4.0",
    "npm": "6.9.0"
  }
}

And my whole project is on https://github.com/powerseed/e-commerce-React The e-commerce folder is for frontend.

Thanks in advance!

powerseed
  • 1,090
  • 3
  • 17
  • 29
  • 1
    You _can_ use import in node. If you're on Node v13 or higher, just add `"type": "module"` to your `package.json` file. – Evan Conrad Jun 05 '20 at 02:39

2 Answers2

2

You can just use require if you want. Here are the details on why there are two ways to bring in files. The difference between "require(x)" and "import x"

georgedum
  • 491
  • 3
  • 10
1

per the Nodejs docs I was able to use the following to run node node-module.js and similar may work in your situation--I'm unfamiliar with the specific node version you're working with currently, but the "type":"module" option would likely allow it--I'm using Nodejs v15 at the moment.

package.json

{
    "type": "module"
}

node-module.js

import * as os from 'os';
import defaultstuff, { stuff } from './node-import-stuff.js';

console.log(123 === stuff, defaultstuff, Object.keys(os));

node-import-stuff.js

export const stuff = 123;
export default stuff;
jimmont
  • 2,304
  • 1
  • 27
  • 29