在用gulp打包项目的时候,
"dev": "NODE_ENV=dev gulp buildDev --gulpfile tasks"
遇到`node`报错
> NODE_ENV=dev gulp buildDev --gulpfile tasks [10:52:59] Requiring external module babel-register ···/tasks/index.js:1 import gulp from ‘gulp‘; ^^^^^^ SyntaxError: Cannot use import statement outside a module
解决:
1. 安装babel包
npm install babel-cli -g npm install -D babel-core npm install -D gulp-babel npm install -D babel-preset-env
2. 在打包命令上增加``--require babel-register``
"dev": "NODE_ENV=dev gulp buildDev --require babel-register --gulpfile tasks",
3. 在package.json添加配置
"babel": { "presets": [ "env" ], "plugins": [ "transform-runtime", "transform-object-rest-spread" ], "babelrc": false }
大功告成!
解决 SyntaxError: Cannot use import statement outside a module 报错问题