使用Express时找不到模块 [英] Modules not found when using express

查看:137
本文介绍了使用Express时找不到模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在电子应用程序中公开REST服务,并且我想在电子应用程序中使用expressJS,因为在教程中我添加了express和@ types/express.我试图公开一个"get",但是当我构建并运行它时,会抛出以下错误.

I want to expose REST service in electron app and I want to use expressJS with electron, as tutorial states I added express and @types/express. I tried to expose a "get " but when I build and run it throws the following.

使用 ng build --prod

./node_modules/cookie-signature/index.js中的
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'

这是我第一次想要做这种事情,所以如果我不知道我是否对不起……

It's the first time i wanna do this kind of things so excuse me if I don't know smth...

package.json:

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"
  }
}

在我添加表达部分之前,该项目一直运行良好.

The project worked perfectly untill I've added the express part, it worked.

  var app = express()

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

推荐答案

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

You have now 2 applications in your solution:

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

1) client application: angular which runs in a browser environment

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

2) server application: express which runs in a node environment

在构建角度应用程序(使用ng build)时,必须确保在角度构建中不包括express应用程序文件.express使用的节点模块在浏览器环境中不可用.

When building the angular application (using ng build) you have to make sure you don't include the express application files in the angular build. The node modules used by express are not available in a browser environment.

您可以这样做:

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

1) move the express source out of the src folder into project root

// 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 .这将继续运行

3) run node server.js from project root. this will keep running

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

4) go to localhost:3000 and see your app

这篇关于使用Express时找不到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆