简体   繁体   中英

import javascript file into json file

Is there any way to import js file into json file. I have js file config.js

module.exports = {id : 'test'}

and I want to use the id key into my json file config.json

const {id } = require('./config.js')
{user_id : id}

This is not something I have seen be done before, in fact I'm not sure if it is actually possible to actually import js file into a json file.

However, I would recommend you convert your json file to a js file instead and then you can have something like:

const { id } = require('./config.js');

const data = {user_id : id};

export default data;

This should work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM