安裝 @babel/core @babel/cli @babel/preset-env 後依然提示 babel-node 不是內部外部命令的問題

前言

日常積累,歡迎指正

1、問題描述

使用 Nuxt.js 官網提供的 新手模板 做 vue SSR 時,該模板初始化不支持 ES Module 爲了添加支持百度到的解決辦法幾乎都是使用 babel-node 解決,並且說 babel-node 是安裝 babel-cli babel-core babel-preset-env 通過在命令中添加 --exec babel-node 即可的,如下:

注意 babel 版本

yarn add @babel/core @babel/cli  @babel/preset-env -D

package.json

{
  "scripts": {
    "dev": "cross-env NODE_ENV=development nodemon server/index.js --watch server --exec babel-node",
    "build": "nuxt build",
    "start": "cross-env NODE_ENV=production node server/index.js --exec babel-node",
    "generate": "nuxt generate",
    "test": "jest"
  }
}

.babelrc

{
  "presets": [
    [
      "@babel/preset-env"
    ]
  ]
}

但是我根據說明操作之後總是報錯babel-node 不是內部外部命令

2、解決方法

是手動安裝一個 babel-node

yarn add @babel/node -D

安裝之後在執行即可解決問題也可以正常使用 ES6 語法,如 ES Module

但是這不是 node 與 babel 結合的最佳方案

3、最佳推薦

You should not be using babel-node in production. It is unnecessarily heavy, with high memory usage due to the cache being stored in memory. You will also always experience a startup performance penalty as the entire app needs to be compiled on the fly.Check out the example Node.js server with Babel for an idea of how to use Babel in a production deployment.

4、關於 @babel/node

@babel/node 與 @babel/core @babel/cli @babel/preset-env 的關係?
獨立 babel/node 能做什麼?
疑惑中

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章