我有一个简单的 Angular 4 应用程序,我想在服务器端使用 Angular Universal(nodeJs 服务器端渲染)运行。 我按照 这些步骤 使用 Angular-cli 帮助配置 Angular Universal,一切都很好,直到我尝试使用 Angular 环境。 当我尝试访问环境常量中的属性时,它在客户端(使用 ngserve)完美呈现,但在服务器端(
ts-node src/server.ts
)它会抛出以下错误:
Error: Cannot find module 'environments/environment'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Dev\code-carama\src\app\auth.service.ts:3:1)
at Module._compile (module.js:571:32)
at Module.m._compile (C:\Dev\code-carama\node_modules\ts-node\src\index.ts:385:23)
at Module._extensions..js (module.js:580:10)
at Object.require.extensions.(anonymous function) [as .ts] (C:\Dev\code-carama\node_modules\ts-node\src\index.ts:388:12)
at Module.load (module.js:488:32)
npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v7.10.0
npm ERR! npm v4.2.0
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! code-carama@0.0.0 start: `ts-node src/server.ts`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the code-carama@0.0.0 start script 'ts-node src/server.ts'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the code-carama package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! ts-node src/server.ts
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs code-carama
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls code-carama
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! C:\Users\dima\AppData\Roaming\npm-cache\_logs\2017-06-05T12_00_03_595Z-debug.log
这是我的
server.ts
:
import 'reflect-metadata';
import 'zone.js/dist/zone-node';
import { platformServer, renderModuleFactory } from '@angular/platform-server';
import { enableProdMode } from '@angular/core';
import { AppServerModuleNgFactory } from '../dist/ngfactory/src/app/app.server.module.ngfactory';
import * as express from 'express';
import { readFileSync } from 'fs';
import { join } from 'path';
const PORT = 4000;
enableProdMode();
const app = express();
let template = readFileSync(join(__dirname, '..', 'dist', 'index.html')).toString();
app.engine('html', (_, options, callback) => {
const opts = { document: template, url: options.req.url };
//it serves html from the compiled angular app from the server
renderModuleFactory(AppServerModuleNgFactory, opts)
.then(html => callback(null, html));
});
app.set('view engine', 'html');
app.set('views', 'src')
app.get('*.*', express.static(join(__dirname, '..', 'dist')));
app.get('*', (req, res) => {
res.render('index', { req });
});
app.listen(PORT, () => {
console.log(`listening on http://localhost:${PORT}!`);
});
尝试访问环境变量的简单服务是
auth.service.ts
:
import { Injectable } from '@angular/core';
import { environment } from 'environments/environment';
@Injectable()
export class AuthService {
private userManager = null;
constructor() {
console.log('AuthService instantiated in environment ' + environment.envName); //this will fail in server-side rendering
}
}
知道 NodeJs(或 Angular Universal)找不到该环境模块是怎么回事吗?
谢谢
在项目 Angular6 中将 'environments/environment' 更改为 'src/environments/environment'
没关系,问题似乎在于,尽管对于客户端渲染该行:
import { environment } from 'environments/environment';
效果很好,对于服务器端渲染,该行必须是:
import { environment } from '../environments/environment';
顺便说一句,我不知道为什么,但默认情况下,对于服务器端渲染,它会选择 PROD 环境文件。
在控制台上:
AuthService instantiated in environment prod
尝试执行此命令
npm run env
,然后执行ng serve
。跟我一起工作吧
app.module.ts
添加此行:
import { environment } from '../environments/environment';
在您的
environment.ts
中添加以下内容:
export const environment = {
production: false
};
你只需要找到你的environment.ts文件的位置。
带角度 14:
如果您在这里有 ts 文件:
src pp uth ducers\index.ts
答案是:
import { environment } from '../../../environments/environment';
你能在src文件夹中看到environments文件夹吗?
然后,你能看到那里的environment.ts和environment.prod.ts吗?
如果是这样,根据您对文件系统的描述,您需要再返回一级。
import { environment } from '../../environments/environment';
在 Angular 15 上,环境文件夹已被删除,但在 15.1 版本上发布了一个解决方案来生成该文件夹:
ng generate environments
它将创建
src/environments/environment.ts
和 src/environments/environment.development.ts
,然后在您的服务或组件中将其导入为:
import { environment } from 'src/environments/environment';
然后你可以像这样使用它:
baseUrl: string = environment.baseUrl;
首先创建环境目录。之后使用 firebaseConfig 创建environment.ts 文件(在我的例子中)。 关键是用 double ('../../) 这一行调用: 从'../../src/environments/environment'导入{环境};
对我来说,一些构建变体
dev
和 prod
有一些不同的 const 名称
export const AppConfig {
而不是
export const environment {
检查是否在 .gitignore 中忽略了它