无法遍历依赖图

发布于 2025-01-10 10:52:48 字数 3095 浏览 0 评论 0原文

我的 Javascript 构建过程最终在文件中插入关键字 require() 。客户端不支持此操作,并会导致控制台错误。我已经按照其他答案添加了 browserify,但是,我又收到了另一个错误(如下)。

附加信息:

我正在使用:

  1. Gulp 4
  2. Node (v14.2.0)

错误:

[16:03:55] Using gulpfile /mnt/c/code/mutationObserver/gulpfile.js
[16:03:55] Starting 'default'...
[16:03:55] Starting 'clean'...
[16:03:55] Finished 'clean' after 10 ms
[16:03:55] Starting 'html'...
[16:03:55] Starting 'js'...
[16:03:55] Starting 'css'...
[16:03:55] Finished 'html' after 46 ms
[16:03:55] Finished 'css' after 51 ms
[16:03:55] 'js' errored after 54 ms
[16:03:55] Error: Can't walk dependency graph: Cannot find module '/mnt/c/code/mutationObserver/src/js/*.js' from '/mnt/c/code/mutationObserver/src/js/_fake.js'
    required by /mnt/c/code/mutationObserver/src/js/_fake.js
    at /mnt/c/code/mutationObserver/node_modules/resolve/lib/async.js:136:35
    at load (/mnt/c/code/mutationObserver/node_modules/resolve/lib/async.js:155:43)
    at onex (/mnt/c/code/mutationObserver/node_modules/resolve/lib/async.js:180:17)
    at /mnt/c/code/mutationObserver/node_modules/resolve/lib/async.js:15:69
    at FSReqCallback.oncomplete (fs.js:175:21)
[16:03:55] 'default' errored after 69 ms

我的整个 Gulpfile.js 如下:

const { series, parallel, watch, src, dest } = require("gulp");
const plumber = require("gulp-plumber");
const del = require("del");
const concat = require("gulp-concat");
const babel = require("gulp-babel");
const sass = require("gulp-sass");
const browserSync = require("browser-sync").create();
const browserify = require('browserify')
const source = require('vinyl-source-stream')


function html() {
  return src("./src/*.html").pipe(dest("./dist"));
}

function css() {
  return src("./src/css/style.scss")
    .pipe(plumber())
    .pipe(sass().on("error", sass.logError))
    .pipe(dest("./dist/css"));
}

function js() {
  return browserify("./src/js/*.js")
    .bundle()
    .pipe(source("main.js"))
    .pipe(plumber())
    .pipe(
      babel({
        presets: ["@babel/env"],
        plugins: ["@babel/transform-runtime"],
      })
    )
    .pipe(dest("./dist/js"));
}

function clean() {
  return del(["./dist"]);
}

function watchFor() {
  browserSync.init({
    server: {
      baseDir: "./dist/",
    },
  });

  // first rerun the function that distributed the css files, then reload the browser
  watch("./src/css/**/*.scss").on("change", css);
  watch("./dist/css/*.css").on("change", browserSync.reload);

  // first rerun the function that distributed the javascript files, then reload the browser
  watch("./src/js/*.js").on("change", js);
  watch("./dist/js/*.js").on("change", browserSync.reload);

  // first rerun the function that writes to the dist folder, then reload the browser
  watch("./src/*.html").on("change", html);
  watch("./dist/*.html").on("change", browserSync.reload);
}

exports.clean = clean;
exports.css = css;
exports.js = js;
exports.html = html;
exports.watch = watch;
exports.default = series(clean, parallel(html, js, css), watchFor);

My Javascript build process ends up inserting the keyword require() in the file. This is not supported in client side and causes a console error. I have added browserify per other SO answers, however, I am in turn getting another error (below).

Additional Information:

I am using:

  1. Gulp 4
  2. Node (v14.2.0)

Error:

[16:03:55] Using gulpfile /mnt/c/code/mutationObserver/gulpfile.js
[16:03:55] Starting 'default'...
[16:03:55] Starting 'clean'...
[16:03:55] Finished 'clean' after 10 ms
[16:03:55] Starting 'html'...
[16:03:55] Starting 'js'...
[16:03:55] Starting 'css'...
[16:03:55] Finished 'html' after 46 ms
[16:03:55] Finished 'css' after 51 ms
[16:03:55] 'js' errored after 54 ms
[16:03:55] Error: Can't walk dependency graph: Cannot find module '/mnt/c/code/mutationObserver/src/js/*.js' from '/mnt/c/code/mutationObserver/src/js/_fake.js'
    required by /mnt/c/code/mutationObserver/src/js/_fake.js
    at /mnt/c/code/mutationObserver/node_modules/resolve/lib/async.js:136:35
    at load (/mnt/c/code/mutationObserver/node_modules/resolve/lib/async.js:155:43)
    at onex (/mnt/c/code/mutationObserver/node_modules/resolve/lib/async.js:180:17)
    at /mnt/c/code/mutationObserver/node_modules/resolve/lib/async.js:15:69
    at FSReqCallback.oncomplete (fs.js:175:21)
[16:03:55] 'default' errored after 69 ms

My entire Gulpfile.js is as follows:

const { series, parallel, watch, src, dest } = require("gulp");
const plumber = require("gulp-plumber");
const del = require("del");
const concat = require("gulp-concat");
const babel = require("gulp-babel");
const sass = require("gulp-sass");
const browserSync = require("browser-sync").create();
const browserify = require('browserify')
const source = require('vinyl-source-stream')


function html() {
  return src("./src/*.html").pipe(dest("./dist"));
}

function css() {
  return src("./src/css/style.scss")
    .pipe(plumber())
    .pipe(sass().on("error", sass.logError))
    .pipe(dest("./dist/css"));
}

function js() {
  return browserify("./src/js/*.js")
    .bundle()
    .pipe(source("main.js"))
    .pipe(plumber())
    .pipe(
      babel({
        presets: ["@babel/env"],
        plugins: ["@babel/transform-runtime"],
      })
    )
    .pipe(dest("./dist/js"));
}

function clean() {
  return del(["./dist"]);
}

function watchFor() {
  browserSync.init({
    server: {
      baseDir: "./dist/",
    },
  });

  // first rerun the function that distributed the css files, then reload the browser
  watch("./src/css/**/*.scss").on("change", css);
  watch("./dist/css/*.css").on("change", browserSync.reload);

  // first rerun the function that distributed the javascript files, then reload the browser
  watch("./src/js/*.js").on("change", js);
  watch("./dist/js/*.js").on("change", browserSync.reload);

  // first rerun the function that writes to the dist folder, then reload the browser
  watch("./src/*.html").on("change", html);
  watch("./dist/*.html").on("change", browserSync.reload);
}

exports.clean = clean;
exports.css = css;
exports.js = js;
exports.html = html;
exports.watch = watch;
exports.default = series(clean, parallel(html, js, css), watchFor);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杯敬自由 2025-01-17 10:52:48

我有同样的错误。这个错误的意思是找不到需要的js文件。确保文件位于“必需”中指定的正确文件夹中,如果位于同一文件夹中,您可能需要添加 ./ 。

browserify main.js -o bundle.js

Error: Can't walk dependency graph: Cannot find module 'srt' from 'C:\code\html1\main.js'

在我的 main.js 错误中,我将: 更改

var srt = require('srt');

为:

var srt = require('./srt');

然后它就起作用了。

I had the same error. This error means that it cannot find the required js file. Make sure that the file is in the correct folder, as specified in "required", and if it's in the same folder, you might need to add ./ .

browserify main.js -o bundle.js

Error: Can't walk dependency graph: Cannot find module 'srt' from 'C:\code\html1\main.js'

In my main.js error, I changed:

var srt = require('srt');

to:

var srt = require('./srt');

And then it worked.

留一抹残留的笑 2025-01-17 10:52:48

我的情况略有不同。我刚刚从 Windows 迁移到 Ubuntu(通过 WSL)。

仔细检查了软件包是否已安装、删除并在本地和全局重新安装,仍然找不到模块。

这以前在 Windows 上有效:

window.Example = require('Example');

结果 Linux 区分大小写 意味着它需要是:

window.Example =需要('示例');

My case was slightly different. I had just moved from Windows to Ubuntu (via WSL).

Double checked the package was installed, removed and re-installed locally and globally, still cannot find module.

This previously worked when on windows:

window.Example = require('Example');

Turns out Linux case sensitivity meant it needed to be:

window.Example = require('example');

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文