Mengapa npm run build for babel menimbulkan kesalahan: babel: src tidak ada

Folder saya adalah src dan saya telah menginstal babel melalui CLI, namun saya masih mendapatkan pesan kesalahan, "babel: src tidak ada"

Berikut rekaman singkat kode saya: https://drive.google.com/file/d/1YgEnJE87c0mEnsefY1xU94cvUedBtGhK/view

Lihat di atas apa yang saya coba.

{
  "name": "notes-app",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "babel src -d lib"
  },
  "author": "Jono Suave",
  "license": "ISC",
  "devDependencies": {
    "@babel/cli": "^7.7.0",
    "@babel/core": "^7.7.2"
  }
}

aplikasi.js

import getNotes from "notes.js"

getNotes()

catatan.js

let getNotes = function() {
    console.log(`Put the time in baby!`)
}

module.exports = getNotes

Saya mengharapkan babel untuk menjalankan dan mengkompilasi kode impor ES6 saya di app.js, tetapi malah menerima kesalahan berikut di terminal:

babel:
  src does not exist
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] build: `babel src -d lib`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/jonosuave/.npm/_logs/2019-11-07T01_56_26_260Z-debug.log

person Jono Suave    schedule 07.11.2019    source sumber
comment
Apakah Anda memiliki package.json di dalam src? Biasanya mereka adalah saudara kandung.   -  person loganfsmyth    schedule 07.11.2019
comment
Terima kasih, itu berhasil! Namun, sekarang saya mendapatkan pesan kesalahan: import { getNotes } from './notes'; ^^^^^^ @loganfsmyth SyntaxError: Tidak dapat menggunakan pernyataan import di luar modul Berikut tautan perekaman layar: drive.google.com/file/d/1lMv59tak_qrMNSNwU7_Cahr7yNuMsn5S/view   -  person Jono Suave    schedule 07.11.2019
comment
Menemukan jawabannya: stackoverflow.com/questions/45854169/   -  person Jono Suave    schedule 07.11.2019


Jawaban (1)


Di node eksperimental, Babel bahkan tidak diperlukan -- hanya perlu menggunakan ekstensi .mjs ke file, bukan .js. Lihat penjelasan dengan contoh: Bagaimana cara menggunakan impor es6 di node?

person Jono Suave    schedule 07.11.2019