How to dynamically import data in a nodejs app?
up vote
0
down vote
favorite
I would like to use require in a node/express app with typescript to import a json. I tried it like this:
const url = `./data/${resource}.json`;
const data = require(url);
but I get the error Cannot find module './data/my-data.json'
.
I'd like to use require instead of an import in order to create the data variable dynamically depending on the value of the resource variable.
node.js json typescript express ejs
add a comment |
up vote
0
down vote
favorite
I would like to use require in a node/express app with typescript to import a json. I tried it like this:
const url = `./data/${resource}.json`;
const data = require(url);
but I get the error Cannot find module './data/my-data.json'
.
I'd like to use require instead of an import in order to create the data variable dynamically depending on the value of the resource variable.
node.js json typescript express ejs
The most likely cause for your problem is the file path to your JSON file relative to your calling code. Can you verify and post the directory structure?
– Paul
7 hours ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I would like to use require in a node/express app with typescript to import a json. I tried it like this:
const url = `./data/${resource}.json`;
const data = require(url);
but I get the error Cannot find module './data/my-data.json'
.
I'd like to use require instead of an import in order to create the data variable dynamically depending on the value of the resource variable.
node.js json typescript express ejs
I would like to use require in a node/express app with typescript to import a json. I tried it like this:
const url = `./data/${resource}.json`;
const data = require(url);
but I get the error Cannot find module './data/my-data.json'
.
I'd like to use require instead of an import in order to create the data variable dynamically depending on the value of the resource variable.
node.js json typescript express ejs
node.js json typescript express ejs
edited 8 hours ago
Gtm
13
13
asked 20 hours ago
Alex
185
185
The most likely cause for your problem is the file path to your JSON file relative to your calling code. Can you verify and post the directory structure?
– Paul
7 hours ago
add a comment |
The most likely cause for your problem is the file path to your JSON file relative to your calling code. Can you verify and post the directory structure?
– Paul
7 hours ago
The most likely cause for your problem is the file path to your JSON file relative to your calling code. Can you verify and post the directory structure?
– Paul
7 hours ago
The most likely cause for your problem is the file path to your JSON file relative to your calling code. Can you verify and post the directory structure?
– Paul
7 hours ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
cost path = require('path');
const url = path.resolve(__dirname, `./data/${resource}.json`);
const data = require(url);
While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
– Tân Nguyễn
7 hours ago
add a comment |
up vote
0
down vote
The require keyword is a special keyword in nodejs. It is used to load modules, and since your json file is not a module, hence the error. Try this, this way you can dynamically load your json.
import fs from 'fs';
const file = fs.readFileSync(`./data/${resource}.json`).toString();
const data = JSON.parse(file);
There may be better ways to write this function, read mode about the fs module here.
Edit: As someone had alredy pointed out, it is actually possible to dynamicallyrequire
json file. Here's how,
import path from 'path';
const uri = path.resolve(__dirname, `<path_to_json_file>`);
const data = require(uri);
However, as a standard practice, use the fs
module to load static assets to your project.
add a comment |
up vote
-1
down vote
import fs from 'fs';
const file = fs.readFileSync(`./data/${resource}.json`).toString();
const data = JSON.parse(file);
This answer is not only incorrect, it parrots an answer given 10 hours earlier
– Paul
7 hours ago
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
cost path = require('path');
const url = path.resolve(__dirname, `./data/${resource}.json`);
const data = require(url);
While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
– Tân Nguyễn
7 hours ago
add a comment |
up vote
0
down vote
cost path = require('path');
const url = path.resolve(__dirname, `./data/${resource}.json`);
const data = require(url);
While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
– Tân Nguyễn
7 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
cost path = require('path');
const url = path.resolve(__dirname, `./data/${resource}.json`);
const data = require(url);
cost path = require('path');
const url = path.resolve(__dirname, `./data/${resource}.json`);
const data = require(url);
answered 11 hours ago
Biplab Malakar
33318
33318
While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
– Tân Nguyễn
7 hours ago
add a comment |
While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
– Tân Nguyễn
7 hours ago
While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
– Tân Nguyễn
7 hours ago
While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
– Tân Nguyễn
7 hours ago
add a comment |
up vote
0
down vote
The require keyword is a special keyword in nodejs. It is used to load modules, and since your json file is not a module, hence the error. Try this, this way you can dynamically load your json.
import fs from 'fs';
const file = fs.readFileSync(`./data/${resource}.json`).toString();
const data = JSON.parse(file);
There may be better ways to write this function, read mode about the fs module here.
Edit: As someone had alredy pointed out, it is actually possible to dynamicallyrequire
json file. Here's how,
import path from 'path';
const uri = path.resolve(__dirname, `<path_to_json_file>`);
const data = require(uri);
However, as a standard practice, use the fs
module to load static assets to your project.
add a comment |
up vote
0
down vote
The require keyword is a special keyword in nodejs. It is used to load modules, and since your json file is not a module, hence the error. Try this, this way you can dynamically load your json.
import fs from 'fs';
const file = fs.readFileSync(`./data/${resource}.json`).toString();
const data = JSON.parse(file);
There may be better ways to write this function, read mode about the fs module here.
Edit: As someone had alredy pointed out, it is actually possible to dynamicallyrequire
json file. Here's how,
import path from 'path';
const uri = path.resolve(__dirname, `<path_to_json_file>`);
const data = require(uri);
However, as a standard practice, use the fs
module to load static assets to your project.
add a comment |
up vote
0
down vote
up vote
0
down vote
The require keyword is a special keyword in nodejs. It is used to load modules, and since your json file is not a module, hence the error. Try this, this way you can dynamically load your json.
import fs from 'fs';
const file = fs.readFileSync(`./data/${resource}.json`).toString();
const data = JSON.parse(file);
There may be better ways to write this function, read mode about the fs module here.
Edit: As someone had alredy pointed out, it is actually possible to dynamicallyrequire
json file. Here's how,
import path from 'path';
const uri = path.resolve(__dirname, `<path_to_json_file>`);
const data = require(uri);
However, as a standard practice, use the fs
module to load static assets to your project.
The require keyword is a special keyword in nodejs. It is used to load modules, and since your json file is not a module, hence the error. Try this, this way you can dynamically load your json.
import fs from 'fs';
const file = fs.readFileSync(`./data/${resource}.json`).toString();
const data = JSON.parse(file);
There may be better ways to write this function, read mode about the fs module here.
Edit: As someone had alredy pointed out, it is actually possible to dynamicallyrequire
json file. Here's how,
import path from 'path';
const uri = path.resolve(__dirname, `<path_to_json_file>`);
const data = require(uri);
However, as a standard practice, use the fs
module to load static assets to your project.
edited 6 hours ago
answered 20 hours ago
Nishkal Kashyap
10716
10716
add a comment |
add a comment |
up vote
-1
down vote
import fs from 'fs';
const file = fs.readFileSync(`./data/${resource}.json`).toString();
const data = JSON.parse(file);
This answer is not only incorrect, it parrots an answer given 10 hours earlier
– Paul
7 hours ago
add a comment |
up vote
-1
down vote
import fs from 'fs';
const file = fs.readFileSync(`./data/${resource}.json`).toString();
const data = JSON.parse(file);
This answer is not only incorrect, it parrots an answer given 10 hours earlier
– Paul
7 hours ago
add a comment |
up vote
-1
down vote
up vote
-1
down vote
import fs from 'fs';
const file = fs.readFileSync(`./data/${resource}.json`).toString();
const data = JSON.parse(file);
import fs from 'fs';
const file = fs.readFileSync(`./data/${resource}.json`).toString();
const data = JSON.parse(file);
import fs from 'fs';
const file = fs.readFileSync(`./data/${resource}.json`).toString();
const data = JSON.parse(file);
import fs from 'fs';
const file = fs.readFileSync(`./data/${resource}.json`).toString();
const data = JSON.parse(file);
edited 6 hours ago
answered 10 hours ago
Gtm
13
13
This answer is not only incorrect, it parrots an answer given 10 hours earlier
– Paul
7 hours ago
add a comment |
This answer is not only incorrect, it parrots an answer given 10 hours earlier
– Paul
7 hours ago
This answer is not only incorrect, it parrots an answer given 10 hours earlier
– Paul
7 hours ago
This answer is not only incorrect, it parrots an answer given 10 hours earlier
– Paul
7 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343722%2fhow-to-dynamically-import-data-in-a-nodejs-app%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The most likely cause for your problem is the file path to your JSON file relative to your calling code. Can you verify and post the directory structure?
– Paul
7 hours ago