0

My requirement is to convert JSON object to string. For this, simply JSON.stringify() doesn't work here because it contains circular references. I searched on the internet then I found a package which does this work for me. But in the usage of the package it is mentioned like const safeJsonStringify = require('safe-json-stringify');. But here the problem is that in Angualar 4 doesn't support require() function anymore. In latest versions of Angular (from version 2), we have import statements. It is like if we have a class is exported in the package then we can directly import that class from the package.

Any suggestions ??

Thanks in advance!!

4
  • u can use require by installing @types/node and including it in tsconfig.json Commented Oct 16, 2018 at 7:11
  • @hana_wujira, Thanks for your answers. Could you tell me how to install require()? and include it in the tsconfig.json? Commented Oct 16, 2018 at 7:19
  • if u are using npm to install - npm install @types/node Commented Oct 16, 2018 at 7:21
  • @hana_wujira, I don't whether it will work or not. But I got the answer. See the below accepted answer. Thanks anyway. Commented Oct 16, 2018 at 9:06

1 Answer 1

3

Try something like:

import * as SafeJsonStringify from 'safe-json-stringify'

This way your import statement can act (in some ways) as a typical require(), as shown in the documentation.

Note: SafeJsonStrinfigy is a value of your choice: you can name it as you like.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much !!. you are fantastic !!. It's working for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.