2015-12-27 20 views
8

Tôi đã làm theo hướng dẫn here để cài đặt babel-cli. Tôi thêm "build": "babel src -d lib" để package.json tôi trong thư mục Tôi muốn chạy nó trong Tuy nhiên, trên chạy, tôi nhận được lỗi này:.Chạy babel-cli từ tập lệnh npm không hoạt động

npm run build 

> [email protected] build /Users/richard/src/ipfs-readme-standard 
> babel src -d lib 

src doesn't exist 

npm ERR! Darwin 14.5.0 
npm ERR! argv "/Users/richard/.nvm/versions/node/v5.0.0/bin/node" "/Users/richard/.nvm/versions/node/v5.0.0/bin/npm" "run" "build" 
npm ERR! node v5.0.0 
npm ERR! npm v3.5.2 
npm ERR! code ELIFECYCLE 
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 'babel src -d lib'. 
npm ERR! Make sure you have the latest version of node.js and npm installed. 
npm ERR! If you do, this is most likely a problem with the ipfs-readme-standard package, 
npm ERR! not with npm itself. 
npm ERR! Tell the author that this fails on your system: 
npm ERR!  babel src -d lib 
npm ERR! You can get information on how to open an issue for this project with: 
npm ERR!  npm bugs ipfs-readme-standard 
npm ERR! Or if that isn't available, you can get their info via: 
npm ERR!  npm owner ls ipfs-readme-standard 
npm ERR! There is likely additional logging output above. 

npm ERR! Please include the following file with any support request: 
npm ERR!  /Users/richard/src/ipfs-readme-standard/npm-debug.log 

Tôi đang ở một mất mát. Không nên tạo ra src? Không có thêm bước nào trên babeljs.io mà tôi đang thiếu.

Trả lời

24

Shouldn't src be generated?

Đây là thư mục chứa tập lệnh mà bạn muốn được chuyển đổi. Nếu nó không tồn tại thì babel sẽ ném lỗi bạn đã đăng.

Ngoài ra, hãy lưu ý những gì nó nói ở dưới cùng của trang web mà bạn liên quan đến:

Pre-6.x, Babel enabled certain transformations by default. However, Babel 6.x does not ship with any transformations enabled. You need to explicitly tell it what transformations to run. The simplest way to do this is by using a preset, such as the ES2015 Preset.

Điều này có nghĩa rằng ngay cả khi bạn tạo một thư mục src và đặt một tập tin chứa mã ES6 trong nó, Babel sẽ vui vẻ chạy, nhưng đầu ra sẽ (gần như) giống với đầu vào.


Đây là ví dụ nhanh về cách thức và hoạt động với babel-cli.

Tạo một dự án, sau đó cài đặt gói babel-cli và ES2015 cài sẵn:

mkdir babeltest && cd babeltest 
touch package.json 
npm install babel-cli babel-preset-es2015 --save-dev 

Tiếp chỉnh sửa package.json:

{ 
    "name": "my-project", 
    "version": "1.0.0", 
    "scripts": { 
    "build": "babel src -d lib" 
    }, 
    "scripts": { 
    "build": "babel --presets es2015 src -d lib" 
    }, 
    "devDependencies": { 
    "babel-cli": "^6.0.0" 
    } 
} 

Chú ý rằng các lệnh trong script NPM là hơi khác với trên babel homepage, cho đến khi chúng tôi đang yêu cầu sử dụng cài đặt trước đã cài đặt.

Tiếp theo tạo một tập tin trong thư mục src:

mkdir src && cd src 
touch main.js 

Trong main.js thêm:

[1,2,3].map(x => x * x) 

Sau đó chạy babel qua NPM:

npm run build 

Và kiểm tra đầu ra trong lib/main.js

"use strict"; 

[1, 2, 3].map(function (x) { 
    return x * x; 
}); 
0

Bạn cũng nhận được lỗi này khi module nút của bạn không được cài đặt, Nếu bạn tải mã từ internet và ngay lập tức thử chạy mã, nó ném các lỗi trên, chỉ cần chạy

npm install 

và sau đó

npm run build // hoặc các lệnh khác sẽ hoạt động

Các vấn đề liên quan