1
votes

The error I receive is:

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)

My directory structure is as follows:

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

My package.json looks like this:

{
  "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://[email protected]/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"
}

My index.js like this:

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

the code in 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');

My 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/'));

});

When I run this without the trouble causing line, everything works fine, it finds the modules and it compiles without a hitch. No problems with not finding the modules.

But when I require that script, the module I required as test from the "parent" script suddenly cannot be found anymore, even though it should still be in the cache by string name.

It does work if I 'require' the files by filename, but that's less than desirable because then I constantly need to check directory nesting.

What causes this and how can I resolve this?

Things I've tried:

setting basedir

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

npm update from 6.4.1 to 6.9.0

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

updated gulp:

+ [email protected]
updated 6 packages in 19.938s
1

1 Answers

1
votes

The solution is rather simple, but not easy to get to the conclusion what causes the error, you have to add node_modules to the 'paths' variable of browserify in your gulpfile.js

// 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
});