spencermountain / spacetime

A lightweight javascript timezone library
http://spacetime.how/
Other
3.97k stars 183 forks source link

Angular Universal - Server Side Rendering - Error Type #300

Closed v4ssi404 closed 2 years ago

v4ssi404 commented 3 years ago

Hello,

i am issuing an error similar to this one #https://github.com/spencermountain/spacetime/issues/144

Basically i am importing spacetime in this way:

import spacetime from 'spacetime;

And this works without problem if i use ng serve but it breaks SSR that gives me:

ERROR TypeError: Cannot read property 'now' of undefined

If i change the import to:

const spacetime = require('spacetime')

the error disappear into the SSR but i get in console the error:

spacetime.now is not a function

Instead if i use this breaks everything.

import * as spacetime from 'spacetime'

Any advice?

I am using Angular 8 and Angular Universal 9.1.1.

My tsconfig.json is:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": ["node_modules/@types"],
    "lib": ["es2018", "dom"]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

Thank you!

spencermountain commented 3 years ago

hey Alberto, I'll admit I'm not a typescript or angular guy, so I may not be much help here. Are you on 6.16.2? I get import * as spacetime working in vanilla node. Do you suspect this is an error with the typescript types, or with babel/whatever angular uses to build? @jecraig any thoughts? cheers

jecraig commented 3 years ago

I tried creating a new Angular Universal app and had no issues.

image image

https://github.com/jecraig/AngularUniversalDemo

v4ssi404 commented 3 years ago

Thank you both for your time.

Yes @spencermountain i was on 6.16.2 but i have now updated to the 6.16.3.

@jecraig i have checked your DemoApp, i tried to change your app settings to angular 8 version but "playing" with your app trying to reproduce the problem i figured out that the problem it's probably in my tsconfig.server.json

This is mine, if i remove module and instead i add "target": "es2015" the error disapper, if you think this could be legit? Thank you.

{
  "extends": "./tsconfig.app.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app-server",
    "module": "commonjs"
  },
  "files": [
    "src/main.server.ts"
  ],
  "angularCompilerOptions": {
    "entryModule": "./src/app/app.server.module#AppServerModule"
  }
}
jecraig commented 3 years ago

Those options are all about setting how the modules are loaded. Frankly, I'm no expert on it, but changing your module resolution is legit.

v4ssi404 commented 3 years ago

Ok thank you! I will try it.