Serving an svg within JSON using NodeJS and ExpressJS - node.js

I have a small reactJS app that rendered objects with svg in them:
import { ReactComponent as F01 } from "./F01.svg";
export const toRender = {
title:"Render this",
text:"Test",
SVGFile: F01
}
I would import this into a component and render it as a component <SVGFile />. All was working 100%.
Now the app got bigger and it became inefficient to store the objects in the frontend. I am trying to send the object from an ExpressJS server. I get an error importing the SVG file.
Here is the route I am using, which works well without SVG files:
import {testObject} from "./testObject";
export const getTestObject= {
path: "/api/render",
method: "get",
handler: (req: Request, res: Response) => {
return res.json(testObject);
},
};
And here is the testObject I am sending:
import { F01 } from "./F01.svg";
export const testObject = {
title:"Render this",
text:"Test",
SVGFile: F01
}
But I get an error right after starting tsnode:
SyntaxError: Unexpected token '<'
at Object.compileFunction (node:vm:360:18)
at wrapSafe (node:internal/modules/cjs/loader:1084:15)
at Module._compile (node:internal/modules/cjs/loader:1119:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
at Object.require.extensions.<computed> [as .js] (D:\desktop\codebases\360ExamPrep\backend\node_modules\ts-node\src\index.ts:1608:43)
at Module.load (node:internal/modules/cjs/loader:1033:32)
at Function.Module._load (node:internal/modules/cjs/loader:868:12)
at Module.require (node:internal/modules/cjs/loader:1057:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> ....
I am not using webpack on the backend.
I tried using const F01 = require('./F01.svg) but that did not help.
Any help is highly appreciated

Related

Node - Migrate postgres DB programmatically using jest globalsetup.js file

I am trying to use testcontainers(https://github.com/testcontainers/testcontainers-node/tree/master/src/modules/postgresql) to spin up a postgres db and use that to run my jest tests.
I used globalsetup.js file to run the container spinup code. The container is spinning successfully, no problem in that, but the issue arises when i try to migrate the db. For Migrating i use typeorm's connection.runMigrations function. Somehow the migration is not working.
All my files, except globalsetup.js is a typescript file.
My connection string looks like this:
createConnection({
url: `postgres://${username}:${encodeURIComponent(password)}#${host}:${port}/${database}`
entities: process.env.TYPEORM_ENTITIES.split(‘,’),
migrations: process.env.TYPEORM_MIGRATIONS?.split(‘,’),
type: 'postgres,
});
TYPEORM_ENTITIES=src/db/entities/**/.ts
TYPEORM_MIGRATIONS=src/db/migrations/.ts
and in my jest.globalSetup.js:
const {createConnection} = require('typeorm')
module.exports = async () => {
/* Code for Container Startup */
process.env.NODE_ENV = ‘test’;
const connection = await createConnection({
url: `postgres://uno-test:dia#localhost:${process.env.TYPEORM_PORT}/cart-test?sslmode=disable`,
entities: process.env.TYPEORM_ENTITIES.split(‘,’),
migrations: process.env.TYPEORM_MIGRATIONS?.split(‘,’),
type: ‘postgres’,
});
await connection
.runMigrations()
.then((value) => console.log(‘Migration Done’, value))
.catch((e) => console.log(e));
await connection
.close()
};
Error Message is:
import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn } from ‘typeorm’;
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1033:15)
at Module._compile (node:internal/modules/cjs/loader:1069:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at /Users/webmaster/CART/cart-portal-service/node_modules/typeorm/util/ImportUtils.js:29:52
at step (/Users/webmaster/CART/cart-portal-service/node_modules/tslib/tslib.js:144:27)
I tried changing the entities and migrations constants to
TYPEORM_ENTITIES=src/db/entities/**/.{ts,js}
TYPEORM_MIGRATIONS=src/db/migrations/.{ts,js}
If i do above chagne, I no longer see the error, but my db is empty as in no tables are created.
Versions:
Node - 16.6.0
Typeorm - 0.2.45
typescript: 4.7.4
jest: 28.1.3

I'm dealing with an issue in Axios (in my code). What do I do?

So, I was trying to create a simple web scraper to extract data from Wikipedia using Node.js, and I used Axios & JSDOM to do it.
When I tried to run it, this was the message I received on my Terminal:
Timothy#Arthurs-MacBook-Air-2 ~ % node scraper.js
/Users/Timothy/scraper.js:1
import axios from 'axios';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47
For reference, I'm using Visual Studio Code on macOS Big Sur, and this is the full reference code for my web scraper (in case it's necessary):
import axios from 'axios';
let jsdom;
jsdom = require('jsdom');
({ jsdom } = jsdom);
axios
.get('https://en.wikipedia.org/wiki/Fauna_of_Scotland');
.then(function ({ data: html }) {
const { document } = new jsdom(html).window;
const nickname = document.querySelector('.mammal');
if (nickname)
console.log(nickname.textContent);
});
.catch(e => {
console.log(e)
});
The Axios documentation doesn't seem to provide any guidance on these error messages, and neither does the JSDOM readme either.
I'd love some help on how to remove the error message, please, as well as where to look further into the matter! Thanks!
i don't have enough reputation to comment
seems the problem is not with axios, check if this can solve your problem: "Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6

NextJS - Unexpected Token Import

While integrating react-syntax-highlighter into my next-js project I've used the following code:
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { okaidia } from "react-syntax-highlighter/dist/esm/styles/prism";
...
<SyntaxHighlighter language="jsx" style={okaidia}>
{some code goes here}
</SyntaxHighlighter>
...
I get the following error upon running npm run dev, but only if I run the page directly.
Unexpected token export
/Users/johndetlefs/repos/tal-gel-framework/node_modules/react-syntax-highlighter/dist/esm/styles/prism/index.js:1
(function (exports, require, module, __filename, __dirname) { export { default as coy } from './coy';
^^^^^^
SyntaxError: Unexpected token export
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.react-syntax-highlighter/dist/esm/styles/prism (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:7242:18)
at __webpack_require__ (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:23:31)
at Module../pages/components.js (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:6839:104)
at __webpack_require__ (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:23:31)
at Object.3 (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:7175:18)
at __webpack_require__ (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:23:31)
at module.exports../js/config/libs/_theme-foundation--colors.js.config.themes.list.name (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:91:18)
If I navigate to the page via another page then everything works great. If I then refresh the page I get the error.
Removing the line
import { okaidia } from "react-syntax-highlighter/dist/esm/styles/prism";
and removing the style attribute from the component fixes everything, but uses the default prism style, which is not the desired outcome.
Looking around I can see people have similar issues, and that the fix probably has something to do with the next.js.config file, and how the css file is being loaded server-side, but I'm not 100% what to do there.
Assuming the next.js.config file is a part of the solution, here are the current contents.
const withSass = require("#zeit/next-sass");
const withCSS = require("#zeit/next-css");
module.exports = withCSS(
withSass({
webpack(config) {
config.module.rules.push({
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
use: {
loader: "url-loader",
options: {
limit: 100000
}
}
});
config.module.rules.push({
test: /\.svg$/,
use: ["#svgr/webpack"]
});
return config;
}
})
);
I've tried both with & without withCSS and the issue stays the same.
Any help would be much appreciated! 👍
After digging around for a while, I checked out the npm packages directory and found that there are two types of dists: cjs & esm. The simple fix is just using the cjs dist instead of the esm dist.
import { darcula } from 'react-syntax-highlighter/dist/cjs/styles/prism';
Hope this helps :)

Extend SchemaDirectiveVisitor To Use Apollo Server Schema Directives in NodeJS

I'm trying to extend SchemaDirectiveVisitor in order to make a custom directive in Apollo Server 2. I'm specifically using the 2.2.6 hapi node module.
Here's my server.js code:
const { ApolloServer } = require('apollo-server-hapi');
const { SchemaDirectiveVisitor } = ApolloServer;
class ViewTemplateGroup extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
console.log('Im calling this directive!');
return;
}
}
When I start up my server I immediately get the following error:
TypeError: Class extends value undefined is not a constructor or null
at Object.<anonymous> (/Users/garrett.kim/Desktop/Projects/Test Web/poc-graphQL-forms-gyk/server.js:36:33)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
at startup (bootstrap_node.js:193:16)
at bootstrap_node.js:617:3
To my knowledge, I'm following the Apollo Server 2 example very closely.
https://www.apollographql.com/docs/apollo-server/features/creating-directives.html
Any help getting directives working would be appreciated.
The ApolloServer class does not have a SchemaDirectiveVisitor property on it; therefore, calling ApolloServer.SchemaDirectiveVisitor results in undefined and a class cannot extend undefined as the error indicates. Just import SchemaDirectiveVisitor directly from the apollo-server-hapi module:
const { ApolloServer, SchemaDirectiveVisitor } = require('apollo-server-hapi')

Vuex without Vue?

I'd like to use Vuex to power a server-side application that doesn't use Vue. Is this possible?
const Vuex = require('vuex');
const store = new Vuex.Store({
state: {
potatoes: 1,
},
getters: {
doublePotatoes(state) {
return state.potatoes * 2;
},
},
mutations: {
addPotato(state) {
state.potatoes += 1;
},
}
});
store.watch((state, getters) => getters.doublePotatoes, console.log);
store.commit("addPotato");
Here's the error I get:
$ node index.js
/private/tmp/vtest/node_modules/vuex/dist/vuex.common.js:99
if (!condition) { throw new Error(("[vuex] " + msg)) }
^
Error: [vuex] must call Vue.use(Vuex) before creating a store instance.
at assert (/private/tmp/vtest/node_modules/vuex/dist/vuex.common.js:99:27)
at new Store (/private/tmp/vtest/node_modules/vuex/dist/vuex.common.js:279:5)
at Object.<anonymous> (/private/tmp/vtest/index.js:3:15)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
at startup (bootstrap_node.js:158:16)
I wound up adding Vue without creating a Vue app:
const Vue = require('vue');
Vue.use(Vuex);
My tiny test store works now. I don't know if Vue.use(Vuex) without creating a Vue app will cause any problems.
Of note for server-side use cases, Vuex doesn't call my watcher for every commit; it batches changes and calls the watcher once. I don't see this documented.

Resources