【问题标题】:Modules not found when using express使用 express 时找不到模块
【发布时间】:2018-11-23 06:23:37
【问题描述】:

我想在电子应用程序中公开 REST 服务,并且我想将 expressJS 与电子一起使用,如教程所述,我添加了 express 和 @types/express。我试图公开一个“get”,但是当我构建并运行它时会抛出以下内容。

ng build --prod一起跑

ERROR in ./node_modules/cookie-signature/index.js
Module not found: Error: Can't resolve 'crypto' in 'app\node_modules\cookie-signature'
ERROR in ./node_modules/etag/index.js
Module not found: Error: Can't resolve 'crypto' in 'app\node_modules\etag'
ERROR in ./node_modules/destroy/index.js
Module not found: Error: Can't resolve 'fs' in 'app\node_modules\destroy'
ERROR in ./node_modules/etag/index.js
Module not found: Error: Can't resolve 'fs' in 'app\node_modules\etag'
ERROR in ./node_modules/express/lib/view.js
Module not found: Error: Can't resolve 'fs' in 'app\node_modules\express\lib'
ERROR in ./node_modules/mime/mime.js
Module not found: Error: Can't resolve 'fs' in 'app\node_modules\mime'
ERROR in ./node_modules/send/index.js
Module not found: Error: Can't resolve 'fs' in 'app\node_modules\send'
ERROR in ./node_modules/express/lib/application.js
Module not found: Error: Can't resolve 'http' in 'app\node_modules\express\lib'
ERROR in ./node_modules/express/lib/response.js
Module not found: Error: Can't resolve 'http' in 'app\node_modules\express\lib'
ERROR in ./node_modules/express/lib/request.js
Module not found: Error: Can't resolve 'http' in 'app\node_modules\express\lib'
ERROR in ./node_modules/express/lib/request.js
Module not found: Error: Can't resolve 'net' in 'app\node_modules\express\lib'
ERROR in ./node_modules/content-disposition/index.js
Module not found: Error: Can't resolve 'path' in 'app\node_modules\content-disposition'
ERROR in ./node_modules/express/lib/view.js
Module not found: Error: Can't resolve 'path' in 'app\node_modules\express\lib'
ERROR in ./node_modules/express/lib/response.js
Module not found: Error: Can't resolve 'path' in 'app\node_modules\express\lib'
ERROR in ./node_modules/express/lib/application.js
Module not found: Error: Can't resolve 'path' in 'app\node_modules\express\lib'
ERROR in ./node_modules/mime/mime.js
Module not found: Error: Can't resolve 'path' in 'app\node_modules\mime'
ERROR in ./node_modules/mime-types/index.js
Module not found: Error: Can't resolve 'path' in 'app\node_modules\mime-types'
ERROR in ./node_modules/send/index.js
Module not found: Error: Can't resolve 'path' in 'app\node_modules\send'
ERROR in ./node_modules/serve-static/index.js
Module not found: Error: Can't resolve 'path' in 'app\node_modules\serve-static'
ERROR in ./node_modules/destroy/index.js
Module not found: Error: Can't resolve 'stream' in 'app\node_modules\destroy'
ERROR in ./node_modules/iconv-lite/lib/streams.js
Module not found: Error: Can't resolve 'stream' in 'app\node_modules\iconv-lite\lib'
ERROR in ./node_modules/iconv-lite/lib/extend-node.js
Module not found: Error: Can't resolve 'stream' in 'app\node_modules\iconv-lite\lib'
ERROR in ./node_modules/send/index.js
Module not found: Error: Can't resolve 'stream' in 'app\node_modules\send'
ERROR in ./node_modules/body-parser/lib/read.js
Module not found: Error: Can't resolve 'zlib' in 'app\node_modules\body-parser\lib'

这是我第一次做这种事情,如果我不知道,请原谅......

package.json:

> {
  "name": "angular-electron",
  "version": "0.0.0",
  "main": "main.js",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "electron": "electron .",
    "electron-build": "ng build --prod && electron ."
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.0.4",
    "@angular/common": "^6.0.4",
    "@angular/compiler": "^6.0.4",
    "@angular/core": "^6.0.4",
    "@angular/forms": "^6.0.4",
    "@angular/http": "^6.0.4",
    "@angular/platform-browser": "^6.0.4",
    "@angular/platform-browser-dynamic": "^6.0.4",
    "@angular/router": "^6.0.4",
    "@types/express": "^4.16.0",
    "angular-svg-round-progressbar": "^2.0.0",
    "core-js": "^2.5.4",
    "express": "^4.16.3",
    "js-sha256": "^0.9.0",
    "rxjs": "^6.2.0",
    "rxjs-compat": "^6.2.0",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.6.6",
    "@angular/cli": "~6.0.7",
    "@angular/compiler-cli": "^6.0.4",
    "@angular/language-service": "^6.0.4",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "electron": "^2.0.2",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "~2.7.2"
  }
}

在我添加了 express 部分之前,该项目运行良好。

  var app = express()

  app.get('/', function (req, res) {
    res.send('Hello World')
  })

【问题讨论】:

  • 哇,那块错误消息破坏了我的屏幕。好像你没有安装所有的包,如果你使用的是NodeJS 8之前的版本,你试过npm install --save吗?
  • NodeJS 8.11.3 并尝试了 npm install --save。和我展示的没什么不同:(
  • 是您的文件名为Package.jsonpackage.json,因为它应该使用小写字母。另一件事可能是您在局部变量中没有指向NodeJS 的正确路径。
  • 抱歉误会,我马上改,是package.json。
  • 尝试删除 node_modules 和 package-lock.json 并使用 npm i 重新安装 deps

标签: javascript typescript express npm electron


【解决方案1】:

我已经尝试了互联网上所有可用的解决方案,但实际上没有任何效果。我的项目使用的是 angular universal,它可以在服务器端呈现应用程序。 一天早上突然我开始收到这个错误,我的项目没有运行。我尝试了 浏览器:{ “fs”:假 } 修复某人建议的。那甚至没有用。我花了一整天的时间。 我开始分析我的旧代码,发现我没有在 package.json 上添加任何东西,所以它不可能是任何其他依赖项。 当我发现有什么问题时,这简直是愚蠢的。我不小心在组件中从 express 导入了 Router

出了什么问题

import { Router } from 'express';

应该是什么

import { Router } from '@angular/router';

【讨论】:

  • 谢谢@biswarup,我犯了同样的错误,从 express 导入路由器。
  • 非常感谢!甚至没有意识到我也这样做了。泰!
  • 谢谢。 - 就我而言,Angular 服务文件中还有其他来自 Express 的导入。
【解决方案2】:

您的解决方案中现在有 2 个应用程序:

1) 客户端应用程序:在浏览器环境中运行的 Angular

2) 服务器应用程序:在节点环境中运行的 express

在构建 Angular 应用程序时(使用 ng build),您必须确保没有在 Angular 构建中包含 express 应用程序文件。 express 使用的节点模块在浏览器环境中不可用。

你可以这样做:

1) 将 express 源码从 src 文件夹移到项目根目录

// server.js
var express = require('express');
var app = express();

app.use(express.static('dist')); // or where the output of the ng-build is placed

app.listen(3000, function() { console.log('Server running on port 3000'); });

2) 运行ng-build

3) 从项目根目录运行node server.js。这将继续运行

4) 转到 localhost:3000 并查看您的应用

【讨论】:

  • 谢谢老兄,两次拯救了我的一天!
【解决方案3】:

检查您的应用并确保它没有自动导入不应该存在的包。

我今天刚遇到这个问题。原因是有时它会在我们使用时自动导入包,我正在发送一个 fetch 请求,它会自动导入 {JSON, Express}。

因此,如果您以某种方式将后端包与 src 文件夹中的前端混合在一起,则会导致问题。

【讨论】:

    猜你喜欢
    • 2016-01-14
    • 2020-12-10
    • 2018-01-27
    • 2017-02-24
    • 1970-01-01
    • 2013-01-09
    • 2014-11-07
    • 2012-05-22
    • 2017-01-23
    相关资源
    最近更新 更多