Gulp and browserify give error: Cannot find module 'jquery' from 'C:....'

Gulp and browserify give error: Cannot find module 'jquery' from 'C:....'

我收到的错误是:

Error: Cannot find module 'jquery' from 'F:...\newstyle\assets\lib\helper\html\img\js'
at C:\Users...\AppData\Roaming\npm\node_modules\browserify\node_modules\browser-resolve\node_modules\resolve\lib\async.js:46:17
at process (C:\Users...\AppData\Roaming\npm\node_modules\browserify\node_modules\browser-resolve\node_modules\resolve\lib\async.js:173:43)
at ondir (C:\Users...\AppData\Roaming\npm\node_modules\browserify\node_modules\browser-resolve\node_modules\resolve\lib\async.js:188:17)
at load (C:\Users...\AppData\Roaming\npm\node_modules\browserify\node_modules\browser-resolve\node_modules\resolve\lib\async.js:69:43)
at onex (C:\Users...\AppData\Roaming\npm\node_modules\browserify\node_modules\browser-resolve\node_modules\resolve\lib\async.js:92:31)
at C:\Users...\AppData\Roaming\npm\node_modules\browserify\node_modules\browser-resolve\node_modules\resolve\lib\async.js:22:47 at FSReqWrap.oncomplete (fs.js:153:21)

我的目录结构如下:

newstyle/assets/npm/index.js
newstyle/assets/npm/package.json
newstyle/assets/npm/gulpfile.js

newstyle/assets/lib/helper/html/img/js/img.module.js

我的 package.json 看起来像这样:

{
  "name": "newstyle",
  "version": "1.0.0",
  "description": "styles and libraries",
  "main": "index.js",
  "dependencies": {
    "@tschallacka/assetmanager": "^1.0.0",
    "@tschallacka/jquery.oc.foundation": "^1.0.2",
    "@tschallacka/jquery.render": "^1.0.0",
    "@tschallacka/jquery.request": "^1.0.0",
    "@tschallacka/oc.foundation.base": "^1.0.1",
    "@tschallacka/oc.foundation.controlutils": "^1.0.1",
    "@tschallacka/oc.foundation.request": "^1.0.0",
    "animate.css": "^3.7.0",
    "bootstrap-less": "^3.3.8",
    "flexslider": "^2.7.2",
    "font-awesome": "^4.7.0",
    "jquery": "^3.4.1",
    "jquery-touchswipe": "^1.6.19",
    "jquery.easing": "^1.4.1",
    "lazysizes": "^4.1.8",
    "liquidslider": "git+https://git@github.com/KevinBatdorf/liquidslider.git",
    "popper.js": "^1.15.0",
    "sweetalert2": "^8.11.1"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

我的index.js是这样的:

require('@tschallacka/oc.foundation.base');
require('@tschallacka/oc.foundation.controlutils');
// ====================== TROUBLE CAUSING LINE!! ==========================
require('../assets/lib/helper/html/img/js/img.module.js');

代码在newstyle/assets/lib/helper/html/img/js/img.module.js

var $ = require('jquery');
var Base = require('@tschallacka/oc.foundation.base');
var controlUtils = require('@tschallacka/oc.foundation.controlutils');

我的gulpfile.js

'use strict';

var browserify = require('browserify');
var gulp = require('gulp');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
var log = require('gulplog');
var less = require('gulp-less');
var cssmin = require('gulp-cssmin');
var rename = require('gulp-rename');

gulp.task('javascript', function () {
  // set up the browserify instance on a task basis
  var b = browserify({
    entries: './index.js', // Source name
    debug: true
  });

  return b.bundle()
    .pipe(source('closure.js'))// Resulting filename
    .pipe(buffer())
    .pipe(sourcemaps.init({loadMaps: true}))
        // Add transformation tasks to the pipeline here.
        .pipe(uglify())
        .on('error', log.error)
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('../js/'));
});

gulp.task('watch', function () {
  gulp.watch('./*.less', ['less']);
});

gulp.task('less', function () {

  return gulp.src('./style.less')
    .pipe(less({
         relativeUrls: true
    }).on('error', function (err) {
      console.log(err);
    }))
    .pipe(cssmin().on('error', function(err) {
      console.log(err);
    }))
    .pipe(rename('closure.css'))
    .pipe(gulp.dest('../css/'));

});

当我 运行 这没有引起问题的行时,一切正常,它找到模块并顺利编译。没有找到模块的问题。

但是当我需要该脚本时,突然找不到我从 "parent" 脚本中作为测试所需的模块,尽管它应该仍按字符串名称存在于缓存中。

如果我按文件名 'require' 文件,它确实有效,但这不太理想,因为我经常需要检查目录嵌套。

是什么原因造成的,我该如何解决?

我尝试过的事情:

设置基目录

var b = browserify({
    entries: './index.js', // Source name
    debug: true,
    basedir: __dirname
  });

npm 从 6.4.1 更新到 6.9.0

Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
npm install -g npm-windows-upgrade
npm-windows-upgrade

已更新gulp:

+ gulp@4.0.2
updated 6 packages in 19.938s

解决方法比较简单,但是不太容易得出错误原因的结论,你必须在你的gulpfile.js中将node_modules添加到browserify的'paths'变量中

// set up the browserify instance on a task basis
var b = browserify({
  entries: './index.js', // Source name
  debug: true,
  paths: ['./node_modules'] // <--- this line here
});