nodejs require路径使用变量报错为啥
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nodejs require路径使用变量报错为啥相关的知识,希望对你有一定的参考价值。
参考技术A $path/index加一杠保险为啥 nodejs 可能找不到“require”?
【中文标题】为啥 nodejs 可能找不到“require”?【英文标题】:Why might nodejs not be able to find "require"?为什么 nodejs 可能找不到“require”? 【发布时间】:2021-04-14 10:48:48 【问题描述】:我使用的是 Win 10。我有一个已经存在几个月的 VisualStudio 2019 项目。
> 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
我在大约一天前发现了一个 nodejs 问题。我有一个正在进行的项目,并决定更新我的 package.json
文件中的依赖项。我跑了npm update
,有几个包确实发生了变化。我有一个 gulpfile,它一直在为项目的生命周期工作,所以运行它但得到了
> 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)
gulpfile 看起来像:
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);
我用谷歌搜索,但只看到有人说节点是浏览器的东西......但我已经在 cmd
终端中手动运行了几个月。
我尝试过的是将节点升级到最新版本。没有帮助。
我通过节点运行 gulpfile:
> 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)
还有什么我应该尝试的吗?
【问题讨论】:
这里的一些不太受欢迎的答案:***.com/questions/31931614/… 可能会有所帮助 你是对的。我的package.json
文件中有一个type: "module"
行,这就是问题所在。删除它解决了我的问题。干杯
这能回答你的问题吗? require is not defined? Node.js
是的,谢谢
【参考方案1】:
感谢@PiRocks,我发现我有一个type: "Module" line in my
package.json` 文件。删除它解决了我的问题。我不记得把它放在那里但是......
【讨论】:
以上是关于nodejs require路径使用变量报错为啥的主要内容,如果未能解决你的问题,请参考以下文章
在vue项目开发中, 为啥图片要用require 引入。。。而不是直接写本地路径
3分钟快速搭建nodejs本地服务器运行测试html/js(转)