Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

node.js - Why might nodejs not be able to find "require"?

I am on Win 10. I have a VisualStudio 2019 project that has existed for several months.

> node -v
v14.15.4

    Note: I was running v14.15.(something less than 4) before the issue below.

> npm -v
6.14.10

> gulp -v
CLI version: 2.3.0
Local version: 4.0.2

I have a nodejs problem that I noticed just a day or so ago. I have an ongoing project and decided to update the dependencies in my package.json file. I ran npm update and a couple of packages did change. I have a gulpfile that has been working for the life of the project and so ran that but got

> gulp
ReferenceError: require is not defined
    at file:///H:/Group_Projects/MyProject/MyProject/gulpfile.js:1:12
    at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
    at async Loader.import (internal/modules/esm/loader.js:166:24)

The gulpfile looks like:

var gulp = require("gulp");
var gclean = require("gulp-clean");

var paths = {
    node: "./node_modules/",
    lib: "./wwwroot/lib/"
};

var packages = {
    "bootstrap": {
        src: paths.node + "bootstrap/dist/**",
        dest: paths.lib + "bootstrap"
    },
    "tempusdominus-core": {
        src: paths.node + "tempusdominus-core/build/**",
        dest: paths.lib + "tempusdominus"
    },
    "tempusdominus-bootstrap-4": {
        src: paths.node + "tempusdominus-bootstrap-4/build/**",
        dest: paths.lib + "tempusdominus"
    },
    "jquery": {
        src: paths.node + "jquery/dist/**",
        dest: paths.lib + "jquery"
    },
    "jquery-validation": {
        src: paths.node + "jquery-validation/dist/**",
        dest: paths.lib + "jquery-validation"
    },
    "jquery-validation-unobtrusive": {
        src: paths.node + "jquery-validation-unobtrusive/dist/**",
        dest: paths.lib + "jquery-validation-unobtrusive"
    },
    "moment": {
        src: paths.node + "moment/min/**",
        dest: paths.lib + "moment"
    },
    "moment-timezone": {
        src: paths.node + "moment-timezone/builds/**",
        dest: paths.lib + "moment-timezone"
    },
    "popper.js": {
        src: paths.node + "@popperjs/core/dist/umd/**",
        dest: paths.lib + "popper.js"
    },
    "js-cookie": {
        src: paths.node + "js-cookie/src/*",
        dest: paths.lib + "js-cookie"
    },
    "fullcalendar": {
        src: paths.node + "@fullcalendar/**",
        dest: paths.lib + "fullcalendar"
    },
    "fontawesomeJS": {
        src: paths.node + "@fortawesome/fontawesome-free/js/**",
        dest: paths.lib + "fontawesome"
    },
    "fontawesomeCSS": {
        src: paths.node + "@fortawesome/fontawesome-free/css/**",
        dest: paths.lib + "fontawesome"
    },
    "luxon": {
        src: paths.node + "luxon/build/global/**",
        dest: paths.lib + "luxon"
    }
};

function clean() {
    return gulp.src(paths.lib + "*", { read: false })
        .pipe(gclean());
}

function build() {
    const merge = require("merge-stream")();

    for (let item in packages) {
        if (packages.hasOwnProperty(item)) {
            console.log(item);
            console.dir(packages[item]);

            merge.add(
                gulp.src(packages[item].src)
                .pipe(gulp.dest(packages[item].dest))
            );
        }
    }

    return merge.isEmpty() ? null : merge;
}

exports.build = build;
exports.clean = clean;
exports.default = gulp.series(clean, build);

I googled but only saw folks saying that node is a browser thing... but I have been running this by hand in a cmd terminal for months.

Among the things I tried were to upgrade node to the latest. Didn't help.

I ran the gulpfile via node:

> node gulpfile.js
file:///H:/Group_Projects/MyProject/MyProject/gulpfile.js:1
var gulp = require("gulp");
           ^

ReferenceError: require is not defined
    at file:///H:/Group_Projects/MyProject/MyProject/gulpfile.js:1:12
    at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
    at async Loader.import (internal/modules/esm/loader.js:166:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)

Anything else I should try?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Thanks to @PiRocks I was able to find that I had a type: "Module" line in my package.json` file. Removing that fixed my issue. I don't remember putting that in there but...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...