Not possible to import *.json files inside TypeScript.
See original GitHub issueI am using aurelia-cli - typescript version. I need to import .json file inside typescript file f.e.import config from "../config/app.json"
.
When I try to build project I am getting an error can’t find file {path to file}/config/app.json.js
I tried to add require js json plugin to aurelia.json file and change my code to import config fom "json!../config/app.json"
(https://github.com/millermedeiros/requirejs-plugins). But without any success. Error:
uid: 9,
name: ‘writeBundles’,
branch: false,
error: [TypeError: Path must be a string. Received undefined],
duration: [ 6, 499384178 ],
time: 1472563256344 }
{ uid: 1,
name: ‘<series>’,
branch: true,
error: [TypeError: Path must be a string. Received undefined],
duration: [ 13, 649611809 ],
time: 1472563256359 }
{ uid: 0,
name: ‘<series>’,
branch: true,
error: [TypeError: Path must be a string. Received undefined],
duration: [ 13, 650647973 ],
time: 1472563256360 }
[TypeError: Path must be a string. Received undefined]
Is there any way to import .json?
Could you please provide me aurelia.json example or other solution to resolve this issue?
Issue Analytics
- State:
- Created 7 years ago
- Comments:26 (11 by maintainers)
We managed to fix this in our aurelia-cli project.
To import use the following syntax:
import * as config from 'text!./folder/config.json';
This should work right away, try writing
alert(config)
for example.Now you will probably see a typescript compilation error in your command line window. To fix this create a file with a
d.ts
extension in yourcustom_typings
folder. The file title doesn’t matter. In this file write:We also wrote a blog post about this here: http://www.curbitssoftware.com/2017/04/04/importing-json-files-aurelia-cli-project/
Use
export
before functions in the foo.js file:in the ts.config :
Now you can import and call that in the ts file:
import someName from '../JS/json_xml.js'; //path to the js file
At last in the class of the ts use what you want:
this.data = someName.someFunction(input);