How to import JSON file to Angular library?
Asked Answered
R

1

9

I'm having trouble importing JSON file to my environment file in Angular 7 library. My environment.ts file looks like this:

import firebase from './firebase.json';

export const environment = {
  production: false,
  firebase,
};

And firebase.json:

{
  "apiKey": "",
  "authDomain": "",
  "databaseURL": "",
  "projectId": "",
  "storageBucket": "",
  "messagingSenderId": ""
}

But unfortunately when running ng build it fails:

> [email protected] build <path-to-project>/sdk
> ng build sdk

Building Angular Package
Building entry point 'sdk'
Compiling TypeScript sources through ngc
Bundling to FESM2015

BUILD ERROR
Unexpected token / in JSON at position 0
SyntaxError: Unexpected token / in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.transform (<path-to-project>/sdk/node_modules/rollup-plugin-json/dist/rollup-plugin-json.cjs.js:18:20)
    at <path-to-project>/sdk/node_modules/rollup/dist/rollup.js:20962:25

Unexpected token / in JSON at position 0
SyntaxError: Unexpected token / in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.transform (<path-to-project>/sdk/node_modules/rollup-plugin-json/dist/rollup-plugin-json.cjs.js:18:20)
    at <path-to-project>/sdk/node_modules/rollup/dist/rollup.js:20962:25
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `ng build sdk`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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!     xxx/.npm/_logs/2019-04-10T13_40_47_486Z-debug.log

I've tried already:

1) Adding to tsconfig.json

"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"noImplicitAny": false,

2) Adding typings.d.ts with

declare module '*.json' {
  const value: any;
  export default value;
}

3) Changing import to require

But no luck so far.

My devDependencies include:

"@angular-devkit/build-ng-packagr": "^0.13.8",
"@angular/cli": "~7.3.6",
"@angular/common": "~7.2.0",
"@angular/compiler": "^7.2.12",
"@angular/compiler-cli": "^7.2.12",
...
Robby answered 10/4, 2019 at 14:25 Comment(8)
Try creating a static JSON file and importing it, probably an issue with your file or JSON format.Bargello
Can you add firebase.json as well?Bargello
Added firebase.json. It was working fine in my app, but I would like to import it to angular library sdk and that's where it fails.Robby
Possible duplicate of Angular 6 - Load JSON from localClouded
Unfortunately none of the answers there solve my problem (which seems to be different).Robby
Your setup looks fine, I wonder if the problem is related to environment file rewrite? Can you try and import the json and log it in another file, not environment?Bargello
So I tried to import JSON file to one of my services in the library. The build still fails. I also tried to make the file completely empty, still the same thing...Robby
May be check the issues at rollup-plugin-json, like this one. If nothing helps open an issue with them, they might help.Bargello
D
10

After a lot of suffering we found that solution here.

It's basically what you've done in the first try, but you need to add one option more.

"resolveJsonModule": true,  
"esModuleInterop": true,
"allowSyntheticDefaultImports": true

Additionally:

"annotateForClosureCompiler": false

Note: for the last prop, that's the same file but instead of adding it to the compilerOptions, add it to angularCompilerOptions

Drus answered 25/4, 2019 at 13:26 Comment(2)
Nope, still have the "Unexpected token / in JSON at position 0"Dab
I add exactly the same issue and disabling annotateForClosureCompiler on angularComplierOptions fix it.Betthel

© 2022 - 2024 — McMap. All rights reserved.