如何在package.json文件中配置babel / preset-env?

时间:2018-12-04 07:03:35

标签: node.js yarnpkg

我试图独自学习使用JavaScript的机器学习,但是即使设置package.json文件也遇到了麻烦。我到处都有Google搜索,并尝试了不同的方法,但我不断遇到以下错误

$ browserify src/index.js --node -o dist/index.js -t [ babelify --presets [ env ] ]
TypeError: Cannot read property 'bindings' of null while parsing file: C:\Users\Brian\Desktop\MLinJSBook\Ch1-Ex1\src\greeting.js

这是我的 这是我的package.json文件

{
  "name": "Ch1-Ex1",
  "version": "1.0.0",
  "main": "src/index.js",
  "license": "MIT",
  "scripts": {
    "build-web": "browserify src/index.js -o dist/index.js -t [ babelify --presets [ env ] ]",
    "build-cli": "browserify src/index.js --node -o dist/index.js -t [ babelify --presets [ env ] ]",
    "start": "yarn build-cli && node dist/index.js"
  },
  "devDependencies": {
    "@babel/cli": "^7.2.0",
    "@babel/preset-env": "^7.2.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react-native": "^4.0.1",
    "babelify": "^10.0.0",
    "browserify": "^16.2.3"
  },
  "dependencies": {
    "@babel/core": "^7.2.0"
  }
}

还有我的代码greetingg.js

const greeting = name => 'Hello, ' + name + '!';
export default greeting;

index.js

import greeting from './greeting';
console.log(greeting(process.argv[2] || 'world'));

我认为我没有正确配置babel / preset-env,但是我尝试的所有方法都无法正常工作。任何帮助深表感谢!

1 个答案:

答案 0 :(得分:0)

如果您使用Babel 7,则必须更新命令以在预设中使用"@babel/preset-env"而不是env

$ browserify src/index.js --node -o dist/index.js -t [ babelify --presets ["@babel/preset-env" ] ]

来源:https://babeljs.io/docs/en/env/