I have the following import statements in my Cloudflare Worker project, to import a html file as a string constant :
import userAgentJsch from './userAgentJsch';
import { parse } from 'cookie';
import test from '.test.html';
It fails with the error:
Module not found: Error: Can't resolve '.test.html' in
I was reading about needing to configure my webpack.config.js file, but I don't have such a file.
Can you please help? I just want to keep my html file separated from the js files, but I can't find a solution I can understand.
View folder structure is used for rendering templates no doubts
to read files in nodejs use this :
const fs = require("fs");
const files = fs.readFileSync("new.html");
and to import
var htmlContent = require('path/to/html/file.html');
Related
Lets suppose there are two .proto files, file hello.proto and file core.proto.
File core.proto is imported with import statement like
import "core.proto";
content of files:
hello.proto
syntax = "proto3";
import "core.proto";
message GetBalance {
string address = 1;
}
core.proto
syntax = "proto3";
message Block {
string chain = 1;
}
I run protoc with
protoc --proto_path=./ --js_out=import_style=commonjs,binary:./ *.proto
Javascript files are generated, but because of "import" statement there is something like
goog.object.extend(proto, core_pb);
and thats a problem because when I try to import this javascript file into my code,
import * as hello_pb from './hello_pb'
it does not work, it says "proto is not defined" it is a reference error.
What am I doing wrong?
Thanks!
While this answer will not directly solve your import problem, it may give you a workaround.
An alternative to "compiling" protobuf into JS is to use protobufjs which implements protobuf and can handle loading arbitrary .proto files. It has its disadvantages (not as lightweight as your solution could be) but it works well.
Assuming you distribute your .proto in your environment (service, app, whatever it is) importing protobufjs and deserializing a message would be something like:
import { loadSync } from 'protobufjs';
...
const protoRoot = loadSync('path/to/hello.proto'));
const Hello = protoRoot.lookupType('Hello'); // can be some_namespace.Hello
...
let message = Hello.decode(buffer);
Most (if not all) datatypes and encoding/decoding is supported. Check out protobuf project page.
i am having a server side rendered node-express-js project ..
i have a js file which contains an object , when i am trying to import that file in backend controller files by doing
const _something = require(directory name)
i am able to import it properly
but when i am trying to import the same file in my front-end assets files i am getting an error as following
Uncaught SyntaxError: Cannot use import statement outside a module
on frontend js file i am importing it as :-
require {something} from '../../dir_name'
here is my export
PoStatus = {
"Work Order Raised":"1",
"Pending Request Approval":"2",
"Pending Quotes":"3",
"Quote Pending Approver":"4",
"Pending Purchase Order":"5",
"Purchase Order Raised":"6",
"Purchase Order Closed":"7",
"Work Order Closed":"8",
"Rejected":"9",
"Quote Pending Proc. Mgr.":"10"
}
module.exports={PoStatus}
and my import is :-
import {PoStatus} from '../../../constants/poStatus'
function approveReturnRejectQuote(status) {
$.confirm({
title: 'Confirm!',
content: `Are you sure you want to $
here is a snapshot of my directory
https://i.stack.imgur.com/sIZQy.png
so i am exporting from constants/poStatus to assets/patra/js/quotesDetails
and i am getting a big fat error . however my IDE is giving suggestions regarding what object is imported and what key value pairs it holds but browser throws an error
In nodeJs (js running on Desktop) to import and export you must use:
require('here the path')
module.exports = { /* variables to export */ }
And in Js (js running on browser)
import 'here the path'
export { /*here variables*/}
if you want to use import in nodeJs you must config with webpack: https://webpack.js.org/guides/getting-started/ to use moderJs (with import and export) then it will transform to oldJs
My recomendation:
If you are building something simple use syntax from nodeJs
If you build something more complex you must configure webpack
I have a vanilla js project that I am building with parcel. In project I have a directory with a lot of json files that I would like to use in one of my modules. I have tried to import them like I would import js files but that didn't work.
import * as regions from './Polygons'
How could I import all the files and iterate over them like you would iterate over an array of objects?
import only works if the thing you are importing is an ES6 module. Just use require in a try-catch:
import fs
dir = './Polygons'
allFiles = fs.readdirSync(dir).map (filename) ->
try
json = require(dir + '/' + filename)
catch e
error = e
return { filename, json, error }
I am building a desktop application using Electorn and angular 8. I am trying to import a javascript file in index.html which has content like the following.
import ipcRenderer from 'electron';
import {
START_NOTIFICATION_SERVICE,
NOTIFICATION_SERVICE_STARTED,
NOTIFICATION_SERVICE_ERROR,
NOTIFICATION_RECEIVED,
TOKEN_UPDATED,
} from 'electron-push-receiver/src/constants';
So when I use the above code I get error Uncaught SyntaxError: Unexpected identifier.
and When I use following code I get Uncaught ReferenceError: require is not defined
const { ipcRenderer } = require ('electron')
const {
START_NOTIFICATION_SERVICE,
NOTIFICATION_SERVICE_STARTED,
NOTIFICATION_SERVICE_ERROR,
NOTIFICATION_RECEIVED,
TOKEN_UPDATED,
} = require ('electron-push-receiver/src/constants')
What could be the solution?
All the angular imports work with first snippets above. they do not have require. So I am assuming the first snippet should work as I am importing it in angular.
I am importing the file by specifying the following in the angular.json file.
"scripts": [
"../path/to/.js",
]
For that import for the ipcRenderer to work you have to establish it like so:
import { ipcRenderer } from "electron";
ipcRenderer being a module that you are pulling out of the electron library.
I'm trying to import the constans from one JS file to another JS file.
Consider the following files:
Constans file (consts.js):
const cars = ["honda","mitsubishi","mercedes"];
export { cars };
Main javascript file (main.js):
import { cars } from './consts.js';
let initNumberOfCars = 0;
let speed = 80;
...
But I get the following error on line 1 at main.js:
Uncaught SyntaxError: Unexpected token {
How should I import the consts from one js file to another one?
Your code should work as is. It must be a Babel issue. If you don't have babel you'll need to look into setting it up with Webpack or Parcel for minimal config.
https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export
https://parceljs.org/
https://webpack.js.org/
https://babeljs.io/