1

I'm trying to put my application into production. But, the teacher who was teaching put it for me, the lib 'sucrase' to use syntax of "import from" instead of "require". Only now Heroku is complaining that he doesn't understand the syntax "import". Is there anything I can do about line of code and configuration? Or do I have to change all "import from" to "require"? ... Lower the logs ...

logs in Heroku

2020-06-27T20:21:41.876688+00:00 app[web.1]: /app/src/server.js:1
2020-06-27T20:21:41.876689+00:00 app[web.1]: import app from './app';
2020-06-27T20:21:41.876690+00:00 app[web.1]: ^^^^^^
2020-06-27T20:21:41.876690+00:00 app[web.1]: 
2020-06-27T20:21:41.876691+00:00 app[web.1]: SyntaxError: Cannot use import statement outside a module
2020-06-27T20:21:41.876691+00:00 app[web.1]: at wrapSafe (internal/modules/cjs/loader.js:1054:16)
2020-06-27T20:21:41.876692+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1102:27)

My package.json

{
  "name": "futs",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "nodemon src/server.js", 
   
    / ^ ^ ^ Below at the code is a configuration about the 'nodemon' that the teacher gave me
   
   "dev:debug": "nodemon --inspect src/server.js"
  },
  "dependencies": {
    "aws-sdk": "^2.704.0",
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "date-fns": "^2.14.0",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "multer": "^1.4.2",
    "multer-s3": "^2.9.0",
    "nodemailer": "^6.4.10",
    "pg": "^8.2.1",
    "sequelize": "^5.21.13",
    "sequelize-cli": "^5.5.1",
    "uuid": "^8.1.0",
    "yup": "^0.29.1"
  },
  "devDependencies": {
    "eslint": "^6.8.0",
    "eslint-config-airbnb-base": "^14.1.0",
    "eslint-config-prettier": "^6.11.0",
    "eslint-plugin-import": "^2.20.2",
    "eslint-plugin-prettier": "^3.1.3",
    "nodemon": "^2.0.4",
    "prettier": "^2.0.5",
    "sucrase": "^3.15.0"
  }
}

nodemon.json

{
  "execMap":{
    "js": "node -r sucrase/register"
  }
}

I don't know how to run to deploy, if anyone can help me

nastari
  • 51
  • 6
  • For node version > 13 you can set "type": "module" in your package.json. For Node version > 12 and < 13, you can set "type": "module" in your package.json. And you need to run node with the --experimental-modules flag.. { "scripts": { "start": "node --experimental-modules src/server.js" }, "type": "module" } – Sandeep Kumar Jun 27 '20 at 21:17

2 Answers2

1

As you see in the error log : Cannot use import statement outside a module.

import statements are permitted only in ES modules not CommonJS see more about that here :

However you can make Node treat your file as a ES module you need to :

  • add "type": "module" to package.json .
  • Add --experimental-modules flag upon running your app(not necessary with Node 13.2.0+ so check your node version with node --version)

as follow :

// package.json
{
  "type": "module"
}

take a look here for more information .

OR : Alternatively you can use the .mjs instead of .jsextension .

SOURCES :

  1. SyntaxError: Cannot use import statement outside a module
  2. https://nodejs.org/api/esm.html .
  3. https://nodejs.org/api/esm.html#esm_enabling .
  4. https://nodejs.org/api/esm.html#esm_code_import_code_statements
mhannani
  • 492
  • 1
  • 5
  • 18
0

To build my app using sucrase I use:

  sucrase ./src -d ./dist --transforms imports

This is my package.json file:

  "scripts": {
    "dev": "nodemon src/server.js",
    "build": "sucrase ./src -d ./dist --transforms imports",
    "start": "node dist/server.js"
  },
Dharman
  • 30,962
  • 25
  • 85
  • 135