简体   繁体   English

Babel / Npm-本地与全局错误

[英]Babel/Npm - local vs global error

I'm trying to transpile an ES6 file using Babel - but NPM doesn't like the command babel src --out-dir output which is run using a script in my package.json. 我正在尝试使用Babel转换ES6文件-但是NPM不喜欢使用package.json中的脚本运行的babel src --out-dir命令输出

However , when I install Babel globally and run the same command without using the script, it works just fine. 但是 ,当我全局安装Babel并在不使用脚本的情况下运行同一命令时,它就可以正常工作。

I would like to avoid installing Babel globally if I don't have to. 如果不需要,我想避免在全球范围内安装Babel。

Error (see debug log below) TypeError: src/person.js: Expected 'input' to be a 'string', got 'number' And looks like the command babel src --out-dir output "fails on your system" 错误(请参见下面的调试日志) TypeError:src / person.js:预期“输入”为“字符串”,得到“数字”,并且看起来像命令babel src --out-dir输出 “系统失败”

Project setup 项目设置

-> root
   -> node_modules
   -> output
   -> src
      - person.js
   - .babelrc
   - package.json

package.json package.json

{
  "name": "Test",
  "version": "0.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "babel src --out-dir output"
  },
  "author": "Me",
  "license": "UNLICENSED",

  "devDependencies": {
    "babel-cli": "~6.11.4",
    "babel-preset-es2015": "^6.9.0"
  }

}

person.js person.js

class Person {
    constructor(name) {
        this.name = name;
    }
}

.babelrc .babelrc

{
  "presets": ["es2015"]
}

Debug log 调试日志

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files (x86)\\node.exe',
1 verbose cli   'C:\\Program Files (x86)\\node_modules\\npm\\bin\\npmcli.js',
1 verbose cli   'run',
1 verbose cli   'build' ]
2 info using npm@2.15.8
3 info using node@v4.4.7
4 verbose run-script [ 'prebuild', 'build', 'postbuild' ]
5 info prebuild Codemode@0.0.0
6 info build Test@0.0.0
7 verbose unsafe-perm in lifecycle true
8 info Test@0.0.0 Failed to exec build script
9 verbose stack Error: Test@0.0.0 build: `babel src --out-dir output`
9 verbose stack Exit status 1
9 verbose stack     at EventEmitter.<anonymous> (C:\Program Files (x86)\node_modules\npm\lib\utils\lifecycle.js:217:16)
9 verbose stack     at emitTwo (events.js:87:13)
9 verbose stack     at EventEmitter.emit (events.js:172:7)
9 verbose stack     at ChildProcess.<anonymous> (C:\Program Files (x86)\node_modules\npm\lib\utils\spawn.js:24:14)
9 verbose stack     at emitTwo (events.js:87:13)
9 verbose stack     at ChildProcess.emit (events.js:172:7)
9 verbose stack     at maybeClose (internal/child_process.js:827:16)
9 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
10 verbose pkgid Codemode@0.0.0
11 verbose cwd C:\Users\me\Dev\Test
12 error Windows_NT 6.1.7601
13 error argv "C:\\Program Files (x86)\\node.exe" "C:\\Program Files (x86)\\node_modules\\npm\\bin\\npm-cli.js" "run" "build"
14 error node v4.4.7
15 error npm  v2.15.8
16 error code ELIFECYCLE
17 error Test@0.0.0 build: `babel src --out-dir output`
17 error Exit status 1
18 error Failed at the Test@0.0.0 build script 'babel src --out-dir output'.
18 error This is most likely a problem with the Test package,
18 error not with npm itself.
18 error Tell the author that this fails on your system:
18 error     babel src --out-dir output
18 error You can get information on how to open an issue for this project with:
18 error     npm bugs Test
18 error Or if that isn't available, you can get their info via:
18 error
18 error     npm owner ls Test
18 error There is likely additional logging output above.
19 verbose exit [ 1, true ]

I'm really not sure where this is going wrong, and can't find any posts on the error. 我真的不确定这出了什么问题,也找不到关于该错误的任何帖子。

Thanks for your help 谢谢你的帮助

Problem solved (by fluke)! 问题解决了(通过福禄克)!

I'm still unsure what the issue was, however I have since added webpack to my project, and added a further script to run the webpack command. 我仍然不确定问题是什么,但是自那以后我就将webpack添加到我的项目中,并添加了另一个脚本来运行webpack命令。 This worked fine, so I tried the npm run build command again and it worked (I checked and babel-cli -g was uninstalled too). 这个工作很好,所以我再次尝试了npm run build命令,它也工作了(我检查了一下,并且babel-cli -g也被卸载了)。

If anyone can shed any light on what went wrong, that would be great!!! 如果任何人都可以找出问题出在哪里,那就太好了!!!

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM