我无法将JSON文件导入Angular 7库中的环境文件。
我的environment.ts
文件如下所示:
import firebase from './firebase.json';
export const environment = {
production: false,
firebase,
};
还有firebase.json
:
{
"apiKey": "",
"authDomain": "",
"databaseURL": "",
"projectId": "",
"storageBucket": "",
"messagingSenderId": ""
}
但是不幸的是,运行ng build
时它失败了:
> sdk@0.0.0 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! sdk@0.0.0 build: `ng build sdk`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the sdk@0.0.0 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
我已经尝试过:
1)添加到tsconfig.json
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"noImplicitAny": false,
2)将typings.d.ts
添加到
declare module '*.json' {
const value: any;
export default value;
}
3)将import
更改为require
但到目前为止还没有运气。
我的devDependencies包括:
"@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",
...
答案 0 :(得分:1)
经历了许多苦难之后,我们找到了解决方法here。
基本上,这是您在第一次尝试中所做的,但是您需要再添加一个选项。
"resolveJsonModule": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
另外:
annotateForClosureCompiler: false
注意:对于最后一个道具,它是相同的文件,但是没有将其添加到compilerOptions
,而是将其添加到angularCompilerOptions