找不到Babel命令 [英] Babel command not found

查看:461
本文介绍了找不到Babel命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照Babel 入门" 的说明安装了babel-cli工具.页面.

I have installed the babel-cli tool as explained by the Babel 'getting started' page.

在我的项目文件夹中的终端上:

From a terminal inside my project folder:

npm install --save-dev babel-cli

在此之后,有一个带有babel-cli文件夹的node_modules目录,但是没有创建package.json. npm还显示以下错误:

After this, there is a node_modules directory with a babel-cli folder, but there is no package.json created. npm also shows the following error:

npm WARN enoent ENOENT: no such file or directory, open '/Users/MyName/Sites/Tutorials/Babel2/package.json

当尝试运行babel时,我得到了:

When trying to run babel, I get this:

babel src -d lib
-bash: babel: command not found

我安装了最新版本的nodejs/npm.我已经运行了npm update -g,并且已经编辑了我的 .bash_profile 文件,使其包括:

I have the latest version of nodejs/npm installed. I have run npm update -g, and I have edited my .bash_profile file to include:

export PATH=$PATH:/Users/MyName/npm/bin
export PATH=/usr/local/share/npm/bin:$PATH

我还没有使用其他npm工具(例如browserify)经历过这种情况.为什么通天塔无法识别?

I have not experienced this with other npm tools such as browserify. Why is babel not recognized?

推荐答案

这里有两个问题.首先,您需要一个package.json文件.告诉npm无人安装将抛出npm WARN enoent ENOENT: no such file or directory错误.在项目目录中,运行npm init为项目生成一个package.json文件.

There are two problems here. First, you need a package.json file. Telling npm to install without one will throw the npm WARN enoent ENOENT: no such file or directory error. In your project directory, run npm init to generate a package.json file for the project.

第二,可能找不到本地二进制文件,因为本地./node_modules/.bin不在$PATH中. 如何使用在node_modules中本地安装的软件包?中有一些解决方案,但仅打包babel- npm脚本中的cli命令.之所以可行,是因为npm runnpm bin(node_modules/.bin)的输出添加到提供给脚本的PATH中.

Second, local binaries probably aren't found because the local ./node_modules/.bin is not in $PATH. There are some solutions in How to use package installed locally in node_modules?, but it might be easier to just wrap your babel-cli commands in npm scripts. This works because npm run adds the output of npm bin (node_modules/.bin) to the PATH provided to scripts.

这是一个简化的示例package.json,该示例返回本地安装的babel-cli版本:

Here's a stripped-down example package.json which returns the locally installed babel-cli version:

{
  "scripts": {
    "babel-version": "babel --version"
  },
  "devDependencies": {
    "babel-cli": "^6.6.5"
  }
}

使用以下命令调用脚本:npm run babel-version.

Call the script with this command: npm run babel-version.

在package.json中放置脚本非常有用,但经常被忽略.文档中的更多内容: npm如何处理脚本"字段

Putting scripts in package.json is quite useful but often overlooked. Much more in the docs: How npm handles the "scripts" field

这篇关于找不到Babel命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆