【问题标题】:Gulp build is not working since devDependencies are not installed when NODE_ENV=productionGulp 构建不起作用,因为当 NODE_ENV=production 时没有安装 devDependencies
【发布时间】:2017-03-30 09:36:09
【问题描述】:

我有一个应用程序,我正在尝试执行npm install && 然后是gulp build。除非我给出 NODE_ENV=production,否则它工作正常,因为 npm 不会安装 devDependencies 中列出的模块。

以下是一些sn-ps。

"devDependencies": {
    "aliasify": "^2.0.0",
    "babel": "^6.5.2",
    "babel-plugin-__coverage__": "^11.0.0",
    "babel-polyfill": "^6.16.0",
    "babel-preset-es2015": "^6.16.0",
    "babelify": "^7.3.0"
}

错误

[13:10:13] Failed to load external module babel-register
[13:10:13] Failed to load external module babel-core/register
[13:10:13] Failed to load external module babel/register
/path/to/repository/gulpfile.babel.js:1
(function (exports, require, module, __filename, __dirname) { import gulp from 'gulp';
                                                              ^^^^^^
SyntaxError: Unexpected reserved word

由于我正在尝试构建用于生产的应用程序,因此未安装 babel 及其依赖项。我什至无法开始构建。

请提出解决方案。

【问题讨论】:

  • 我认为import gulp from 'gulp';必须在文件/path/to/repository/gulpfile.babel.js的顶部而不是在功能块中
  • 它不在功能块中。我的 gulpfile.babel.js 看起来像这样。 import gulp from 'gulp'; import runSequence from 'run-sequence'; import browserSync from 'browser-sync'; import nodemon from 'gulp-nodemon'; import gulpLoadPlugin from 'gulp-load-plugins'; import './gulp/common'; import './gulp/script'; import './gulp/style'; import './gulp/test'; const gulpSequence = runSequence.use(gulp); const $ = gulpLoadPlugin(); gulp.task('build', (callback) => { gulpSequence('clean', 'preprocess', 'styles', 'scripts', 'resources', 'tmp-del', callback); });

标签: node.js npm gulp babeljs


【解决方案1】:

如果不想安装dev依赖,可以在nodejs已经支持的es5和es6特性中编写gulpfile。

或者您可以在运行 gulp 任务后设置 NODE_ENV。

npm install --only=dev 也将安装 dev 依赖项,而不考虑 NODE_ENV。

【讨论】:

  • 我也想过这样做。但是环境变量是在所有命令运行之前使用 Jenkins 设置的。此外,我已经在 es6 中编写了整个 gulp 代码。我正在寻找一种不需要在 es5 中编写 gulpfile 的解决方案。
  • @AmitDaga nodejs 已经支持很多 es6 特性,用 require 替换 import 就可以了
  • @AmitDaga 你可以试试npm install --only=dev,不管node_env如何,它都会安装dev依赖,但记得也要安装依赖。
  • 看起来更好的解决方案。
猜你喜欢
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2015-06-20
  • 1970-01-01
  • 2014-11-26
  • 2018-12-07
  • 1970-01-01
  • 2019-01-18
相关资源
最近更新 更多