-1

I don't know how to resolve this error when I'm running nodemon server.js I've tried adding --experimental-modules and also "type": "module" but none of it is working for me, should I have

import esm from 'esm'

somwhere?

package.json

{
  "name": "facebook-back",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "server.js"
  },
  "author": "Ethan",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "gridfs-stream": "^1.1.1",
    "mongoose": "^5.11.8",
    "multer": "^1.4.2",
    "multer-gridfs-storage": "^4.2.0",
    "path": "^0.12.7",
    "pusher": "^4.0.2"
  }
}

terminal output

$ nodemon server.js                                                                             
[nodemon] 2.0.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
internal/modules/cjs/loader.js:1149
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/ethanding/Desktop/projects/facebook/facebook-back/server.js
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1149:13)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  code: 'ERR_REQUIRE_ESM'
}
[nodemon] app crashed - waiting for file changes before starting...

server.js

import express from 'express'
import mongoose from 'mongoose'
import cors from 'cors'
import multer from 'multer'
import GridFsStorage from 'multer-gridfs-storage'
import Grid from 'gridfs-stream'
import bodyParser from 'body-parser'
import path from 'path'
import Pusher from 'pusher'

Grid.mongo = mongoose.mongo

// app config
const app = express()
const port = process.env.PORT || 9000

// middlewares
app.use(bodyParser.json())
app.use(cors())

//api routes
app.get('/', (req, res) => res.status(200).send('hello world'))

// listen
app.listen(port, () => console.log(`Listening to port ${port}`))
draco
  • 1
  • 1
  • It isn't clear what the problem is. You should provide a [mcve]. Showing us your package.json and terminal output is all very well, but you left out your JS! Why are you asking about nodemon? Have you successfully run the same program standalone and only have the problem with nodemon? – Quentin Dec 28 '20 at 00:14
  • whats the nodejs version you have on your machine – IAMSTR Dec 28 '20 at 00:20
  • sorry about that, included the server.js file now. I was following a tutorial up until the point where they used nodemon (the way it's used in terminal) to launch the back end of the app, but when I tried to it gave me this notification – draco Dec 28 '20 at 13:45

1 Answers1

-1

If your writing a module: Use .mjs

If not: You have to use a transpiler like babel, webpack or typescript (The latter one I HIGHLY recommend) in order to use emac module syntax (import..., export...)

You can find more information here

Also google is your best friend: NodeJS "Must use import to load ES Module"

As well as node --experimental-modules apple.mjs from https://github.com/nodejs/node/issues/17063

IcyTv
  • 405
  • 4
  • 12