我有一个包含以下行的nodeJS脚本
import StratumClient from "node-stratum-client"
const client = new StratumClient()
我想让脚本按原样工作,而不更改第一行,这会导致此错误
(function (exports, require, module, __filename, __dirname) { import StratumClient from "node-stratum-client"
^^^^^^
SyntaxError: Unexpected token import
所以我按照这里的说明进行操作 - Node error: SyntaxError: Unexpected token import。以下是输出
localhost:node-stratum-client satishp$ npm install --save-dev babel-cli babel-preset-es2015
npm WARN deprecated babel-preset-es2015@6.24.1: Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update!
npm WARN node-stratum-client@0.0.1 No repository field.
+ babel-cli@6.26.0
+ babel-preset-es2015@6.24.1
added 46 packages in 4.642s
localhost:node-stratum-client satishp$ echo '{ "presets": ["es2015"] }' > .babelrc
localhost:node-stratum-client satishp$ ./node_modules/.bin/babel src -d lib
src/StratumClient.js -> lib/StratumClient.js
src/StratumClient.test.js -> lib/StratumClient.test.js
src/example.js -> lib/example.js
src/index.js -> lib/index.js
然而,当我再次运行我的脚本时,我得到了同样的错误。如果我上面做错了,我很好奇。我如何知道我的脚本是否使用babel编译?
localhost:node-stratum-client satishp$ npm install
npm WARN node-stratum-client@0.0.1 No repository field.
up to date in 2.505s
localhost:node-stratum-client satishp$ node run.js
/Users/satishp/Documents/workspace/node-stratum-client/run.js:1
(function (exports, require, module, __filename, __dirname) { import StratumClient from "node-stratum-client"
^^^^^^
SyntaxError: Unexpected token import
at new Script (vm.js:51:7)
at createScript (vm.js:138:10)
at Object.runInThisContext (vm.js:199:10)
at Module._compile (module.js:624:28)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
at startup (bootstrap_node.js:193:16)