将 TypeScript 项目与 Webpack 捆绑时,Node.js 提示“ReferenceError: require is not Define”

2024-05-22

我有一个简单的test.ts我想使用 webpack 捆绑的文件。该文件包含一些代码和以下导入,没有任何错误:

import { from, interval, Observable, of, pipe, Subject, timer, BehaviorSubject, zip, EMPTY } from 'rxjs/index.js'
import { filter, map, mergeMap, delay, concatMap, concatMapTo, concatAll, toArray, catchError, retry, retryWhen, delayWhen, tap, take, switchMap, repeat, scan, withLatestFrom, reduce, expand, exhaustMap, mapTo } from 'rxjs/operators/index.js'
import axios from 'axios'
import cheerio from 'cheerio'
import chalk from 'chalk'

这是我的内容package.json file:

{
  "name": "myproject",
  "version": "0.0.1",
  "description": "",
  "private": "true",
  "type": "module",
  "scripts": {
    "test": "tsc && node dist/test.js",
    "build": "webpack"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^14.14.39",
    "axios": "^0.21.1",
    "chalk": "^4.1.1",
    "cheerio": "^1.0.0-rc.6",
    "rxjs": "^6.6.7",
    "telegraf": "^4.3.0"
  },
  "devDependencies": {
    "webpack": "^5.36.2",
    "webpack-cli": "^4.6.0"
  }
}

The tsconfig.json文件看起来像这样:

{
  "compilerOptions": {
    /* Basic Options */
    "target": "ES2016",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "es2015",                          /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    "allowJs": true,                             /* Allow javascript files to be compiled. */
    "checkJs": true,                             /* Report errors in .js files. */
    "sourceMap": true,                           /* Generates corresponding '.map' file. */
    "outDir": "./dist",                          /* Redirect output structure to the directory. */
    "rootDir": "./src",                          /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */

    /* Strict Type-Checking Options */
    "strict": true,                              /* Enable all strict type-checking options. */

    /* Module Resolution Options */
    "moduleResolution": "node",                  /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    "esModuleInterop": true,                     /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */

    /* Advanced Options */
    "skipLibCheck": true,                        /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true     /* Disallow inconsistently-cased references to the same file. */
  },
  "exclude": ["./dist/**/*"]
}

编译项目时,我收到以下消息:

无法写入文件“d:/asant/Desktop/myproject/webpack.config.js”,因为它会覆盖输入文件。

这就是webpack.config.js file:

const path = require('path')

module.exports = {
    entry: './src/test.ts',
    devtool: 'inline-source-map',
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/
            }
        ]
    },
    resolve: {
        extensions: ['.tsx', '.ts', '.js']
    },
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    }
}

当。。。的时候build运行脚本会输出:

[webpack-cli] Failed to load 'D:\asant\Desktop\myproject\webpack.config.js' config
[webpack-cli] ReferenceError: require is not defined
    at file:///D:/asant/Desktop/myproject/webpack.config.js:1:14
    at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
    at async Loader.import (internal/modules/esm/loader.js:166:24)
    at async loadConfig (D:\asant\Desktop\myproject\node_modules\webpack-cli\lib\webpack-cli.js:1346:35)
    at async WebpackCLI.resolveConfig (D:\asant\Desktop\myproject\node_modules\webpack-cli\lib\webpack-cli.js:1454:38) 
    at async WebpackCLI.createCompiler (D:\asant\Desktop\myproject\node_modules\webpack-cli\lib\webpack-cli.js:1839:22)
    at async WebpackCLI.buildCommand (D:\asant\Desktop\myproject\node_modules\webpack-cli\lib\webpack-cli.js:1954:20)  
    at async Command.<anonymous> (D:\asant\Desktop\myproject\node_modules\webpack-cli\lib\webpack-cli.js:735:25)       
    at async Promise.all (index 1)
    at async Command.<anonymous> (D:\asant\Desktop\myproject\node_modules\webpack-cli\lib\webpack-cli.js:1284:13)      
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] /cdn-cgi/l/email-protection build: `webpack`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] /cdn-cgi/l/email-protection build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\asant\AppData\Roaming\npm-cache\_logs\2021-05-01T14_28_01_191Z-debug.log
The terminal process "C:\WINDOWS\System32\cmd.exe /d /c npm run build" terminated with exit code: 2.

也在运行test将输出:

error TS5055: Cannot write file 'D:/asant/Desktop/myproject/webpack.config.js' because it would overwrite input file.

error TS6059: File 'D:/asant/Desktop/myproject/webpack.config.js' is not under 'rootDir' 'D:/asant/Desktop/myproject/src'. 'rootDir' is expected to contain all source files.
  The file is in the program because:
    Matched by include pattern '**/*' in 'D:/asant/Desktop/myproject/tsconfig.json'


Found 2 errors.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] /cdn-cgi/l/email-protection test: `tsc && node dist/test.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] /cdn-cgi/l/email-protection test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\asant\AppData\Roaming\npm-cache\_logs\2021-05-01T14_29_07_580Z-debug.log
The terminal process "C:\WINDOWS\System32\cmd.exe /d /c npm run test" terminated with exit code: 1.

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 TypeScript 项目与 Webpack 捆绑时,Node.js 提示“ReferenceError: require is not Define” 的相关文章

  • 数字和数字对象有什么区别?

    存储在普通变量中的数字有什么区别 var foo 5 和一个数字对象 var bar new Number 5 数字对象可以用来做什么 A Number对象包含一些有用的方法和属性 例如 数字对象方法 Method Description
  • DOM 元素宽度可以是非整数吗?

    我有一个页面 其 div 元素由 JavaScript 对齐 JavaScript 只是检查一组 div 元素来查找最大值偏移宽度 然后设置所有 div 元素 width成为最大偏移宽度 它在大多数浏览器和区域设置中都能完美运行 但在 Ma
  • 如果重复,则从数组中删除两个值 - JavaScript/jQuery

    我这里有一个数组 var myArr 1 1 2 5 5 7 8 9 9 现在我想删除重复的两个外观 所以想要的结果是not var myArr 1 2 5 7 8 9 but var myArr 2 7 8 基本上我知道如何删除重复项 但
  • 等待两个异步函数完成,然后在 Node.js 中继续

    我正在 Node js 中开发一个应用程序 其中调用异步函数两次 并将值分配给全局变量 问题是我想使用这两个调用的结果来做其他事情 但是这个其他事情不会等待结果被分配 这是我的代码 var a var b let x abcd foo x
  • 如何使用 Angularjs 检查模块中的指令或控制器是否可用

    在 angularjs 中 给定一个模块 如何检查给定一个模块是否存在指令 控制器 我有一个模块 我想知道是否已加载某些特定指令 下面是一些示例代码 var module angular module myModule check if c
  • 在 TypeScript 中迭代对象的键和值

    在纯 JavaScript 中 我们可以迭代对象属性和值 如下所示 const values Object keys obj map key gt obj key 在 TypeScript 中 此语法是错误的 因为 TS 编译器显示以下消息
  • 如何在pm2下运行nuxt?

    我有 2 个 nuxt 项目需要在服务器上运行 每当我在本地运行该应用程序时 它似乎都在使用 npm run dev 但在服务器上 这需要在子进程下运行 所以我使用 pm2 来实现 但是每当我开始使用 pm2 运行相同的 npm 脚本时 该
  • jQuery 显示隐藏左侧滑动面板

    我希望在单击按钮时面板从浏览器的左边缘滑动 并在单击同一按钮 切换 时隐藏面板 Html div class panel div a href class slider arrow show raquo a CSS panel width
  • 在 JavaScript 中对数组进行排序

    我正在尝试对内部包含整数的数组进行排序 例如 var array 123 3 745 4 643 5 643 2 我如何对其进行排序才能返回类似以下内容的内容 array 745 4 643 2 643 5 123 3 您可以将自定义比较函
  • d3js v5 + Topojson v3 关于加入 csv 和 json 的优化

    为了制作地图 我需要将一些值从 csv 导入到 json直接在代码中 为了加载 json 和 csv 文件 我使用 Promise 对象的异步操作 并使用两个循环和一个公共键在 json 文件上添加新属性 for var i 0 i lt
  • 如何使 CSS 样式覆盖 JavaScript 应用的样式

    我动态创建一个 HTML 表 并应用一些样式 var tbl document createElement table tbl id CrewMemberTable document getElementById CrewMemberPan
  • 如何在不知道id的情况下从内页获取父iframe元素?

    让我们想象一下我有这样的东西 div div test html 是空页面 自定义hash属性始终具有不同的值 出于安全原因两个页面位于同一域 iframe 元素的数量和顺序是随机的 我的问
  • 角度按钮单击旋转图标

    我有以下按钮
  • 地理编码服务与 gMap 搜索之间的差异结果

    我想获取此地址的纬度和经度 Boulevard de la Marne Zone industrielle COULOMMIERS 77120 France 我将 api 添加到我的页面 当我使用 gmaps 时 结果很好 但是当我想通过
  • 如何在 Express (NodeJS) 中验证和处理表单

    Express 是否有首选的表单处理和验证库 我真的在寻找与 Django 表单中类似的抽象级别 即模板中的验证和错误报告 如果可以在客户端使用相同的验证 那就太好了 有人用过或者写过什么好东西吗 看起来有一个模块位于https githu
  • HighCharts - 两个 Y 轴,一个具有最大值

    我在使用 Highcharts 时遇到问题 我已将右 Y 轴设置为 min 0 和 max 100 它是 但它不起作用并生成 125 百分比 怎么了 http jsfiddle net SdTcU http jsfiddle net SdT
  • 是否可以对 Flexbox 插入、删除和项目位置进行动画处理?

    这个问题最初是在 2012 年提出的 https stackoverflow com questions 11106876 is it possible to animate flexbox inserts removes但是提供的答案并未
  • 通过 Javascript 填充 ReactJS HTML 表单

    我正在开发一个应用程序 在打开第 3 方网站后 我可以在浏览器上下文中运行我自己的 Javascript 作为一个基于reactjs构建并具有登录表单的示例网站 您可以参考此链接 我正在尝试在reactjs生成的表单中填写用户名和密码 但是
  • 如何从 fetch API 返回 json 响应

    我有一个像这样的函数 check auth fetch Urls check auth credentials include method GET then response gt if response ok return respon
  • localStorage 获取 NULL?

    我不知道为什么 因为我之前已经这样做过并且工作正常 我认为这可能是因为浏览器问题 错误 localStorage setItem foo bar alert localStorage getItem foo 我使用的是 Firefox 3

随机推荐

  • Ruby:创建日期范围

    我正在寻找一种优雅的方式来制作一系列日期时间 例如 def DateRange start time end time period end gt gt results DateRange DateTime new 2013 10 10 1
  • 如何使用 XmlSerializer 在大文档中插入节点

    我有一个很大的 XML 文档 我想使用XmlSerializer类来插入新元素 其内容来自使用 xsd exe 生成的 NET 类实例 这是问题的后续如何使用 XmlSerializer 反序列化大型文档中的节点 https stackov
  • 将数据插入向量时多次调用复制构造函数

    include
  • 尝试克隆一个 git 存储库,但它卡在克隆到中

    我使用的是 Windows 10版本 10 0 19042 内部版本 19042 GIT Ver 2 32当尝试使用 git bash 执行以下命令时git clone depth 1 b carla https github com Ca
  • Allen Browne 的 ConcatRelated() 错误 3061:参数太少

    我正在尝试创建给定仓库的产品列表 Allen Browne 的 ConcatRelated 函数似乎是在链接变量相同时创建列表的经过验证的正确方法 但我无法让它工作 我已将我的信息分解为单个查询 qry Products SELECT qr
  • 如何从 ISI Web of Knowledge 检索有关期刊的信息?

    我正在从事一些预测文章引用计数的工作 我遇到的问题是我需要 ISI Web of Knowledge 中有关期刊的信息 他们逐年收集这些信息 期刊影响因子 特征因子 但无法一次下载所有一年期期刊信息 只有 标记全部 选项 该选项始终标记列表
  • 如何在 C++ 中追加一个整数(用一个整数)

    我想知道是否有人可以告诉我如何在 C 中附加一个整数 与另一个整数 基本上 如果我有一个值为 67 的 int 我如何将它与数字 4 一起附加 以便整数现在为 674 提前致谢 将第一个乘以十的第二个位数次方 然后加上另一个 示例 63 和
  • 如何将 NSString 值 @"3.45" 转换为浮点数?

    如何将 NSString 值 3 45 转换为浮点数 3 45 float fCost NSDecimalNumber decimalNumberWithString 3 45 floatValue NSString val 3 45 fl
  • 分配 @changeset 在 eex 模板中不可用

    我正在尝试学习包含 Ecto Model 的 Phoenix 表单系统 但我遇到了一个无法通过的问题 我创建了一个表格 div class jumbotron div
  • 收益率回报多少?

    我发现自己经常对 IEnumerables 进行查找 以便返回每个结果 有没有办法压缩这样的东西 foreach var subSelector in subSelectors foreach var node in FindSingle
  • Android应用程序开发中的EditText警告

    在 xml 文件中声明 EditText 时 我收到了如下警告 没有标签视图通过 android labelFor id id start 属性指向此文本字段 编辑文本代码是
  • SWT GridLayout 列重叠

    Code final Composite sectionClient toolkit createComposite parent SWT NONE sectionClient setLayout UIHelper getLayoutFor
  • FFMPEG:使用具有复杂滤波器的视频滤波器

    我正在使用fluent ffmpeg用于对视频文件执行批量操作的 Node js 库 视频过滤器裁剪 16 9 输入 添加填充并将字幕刻录到填充中 在下一步中 我想使用复杂的滤镜来覆盖图像作为水印 ff input video mp4 ff
  • 网格 xamarin 形式中的重叠框架

    我正在使用最新的 xamarin 表单 我有一个要求 我需要在网格中的两行之间放置一个框架 我无法使其与绝对布局或弹性布局一起使用 下面是我对网格的简化尝试
  • 更新我的模型然后重新评估 IsValid?

    我将一些值传递给我的控制器操作 一切都绑定得很好 根据设计 表单 POST 中会缺少两个属性 然后我设置了缺失的值 但随后我想验证模型 但它仍然显示为 false 因为 ModelState 似乎没有跟上我的更改 HttpPost Auth
  • 为什么此类方法中的“this”未定义?

    我试图搜索似乎整个互联网 但我仍然对我为微服务编写的 JS 类的问题感到烦恼 仍在学习中 因此 我尝试在实例化对象上调用类方法 根据我的知识和我的 我认为是错误的 单元测试 它应该可以工作 好吧 我将从我收到的错误开始 GET api us
  • Huggingface 长文档摘要

    我预计摘要任务通常会假设长文档 但是 以下文档here https huggingface co course chapter1 3 fw pt 我所做的任何简单摘要调用都表示我的文档太长 gt gt gt summarizer pipel
  • WPF 如何从 DataGrid 转换为 DataTable?

    嗨 我正在关注这个guide http www wpf tutorial com datagrid control details row 以了解如何使用 DataGrid 我遇到的问题是如何将 DataGrid 中的数据转换为 DataT
  • 如何在 Unity 中对齐“轨道”或模块化对象?

    我正在开发一个简单的游戏 用户可以在其中放置不同但模块化的对象 例如 轨道 道路等 我的问题是 当将一个物体靠近另一个物体时 如何匹配和放置不同的物体 我的第一种方法是为每个模块对象创建一个隐藏的子对象 一个盒子 并将其放在可以放置其他对象
  • 将 TypeScript 项目与 Webpack 捆绑时,Node.js 提示“ReferenceError: require is not Define”

    我有一个简单的test ts我想使用 webpack 捆绑的文件 该文件包含一些代码和以下导入 没有任何错误 import from interval Observable of pipe Subject timer BehaviorSub