Как запустить ECMAScript (файлы mjs) с помощью nodemon? - PullRequest
1 голос
/ 12 июля 2019

Я могу запускать файлы mjs с помощью nodejs, используя флаг --experimental-modules.

node --experimental-modules index.mjs

package.json:

{
    "name": "mjs-tests",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "dev": "nodemon index.mjs"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
        "chalk": "^2.4.2",
        "uuid": "^3.3.2"
    },
    "devDependencies": {
        "nodemon": "^1.19.1"
    }
}

И index.mjs



import http from 'http'

const server = http.createServer((req, res) => {
    res.end('hello')
})

const PORT = 5000
server.listen(PORT, () => {
    console.log(`?‍♀️ Server is running at http://localhost:${PORT}`)
})

Но если я попытаюсь

npm run dev

или (с глобально установленным nodemon)

nodemon index.mjs

Я получаю эту ошибку

[nodemon] 1.19.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node index.mjs`
internal/modules/cjs/loader.js:821
  throw new ERR_REQUIRE_ESM(filename);
  ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module

Итак, как мне включить поддержку ECMAScript в nodemon? Или я должен использовать что-то вроде esm ?

1 Ответ

1 голос
/ 12 июля 2019

Offcourse yes , Все, что вам нужно, чтобы изменить свой пакет. Json

  "scripts": {
        "dev": "nodemon --experimental-modules index.mjs"
    },

enter image description here

...