Challenges with the ASP .Net Core 3.1 octokit rest npm package

I am utilizing npm along with a gulpfile.js to export npm packages to a 'lib' folder under 'wwwroot'. This process works smoothly, and whenever I update a specific npm package listed in my gulpfile.js, it automatically pushes the contents to the 'lib' folder.

An issue arose when I used to have a manually extracted copy of ocktokit-rest for querying the public API to retrieve repo data. Recently, this manual setup stopped working, presumably due to GitHub updating their API which caused compatibility issues with the old version of ocktokit-rest. To address this, I installed @ocktokit/rest version 18.0.9 via npm package.json, resulting in the creation of the following directory:

~/lib/@octokit/rest/

As per the documentation, I need to reference one of the index.js files within this directory. However, due to Razor not supporting the @ symbol in paths, I had to use the following in my _layout.cshtml:

<script src="@Url.Content("~/lib/@octokit/rest/dist-src/index.js")" type="module"></script>

To handle import statement issues in the index.js file, I added type="module". The content of the index.javascript file at the specified route is as follows:

import { Octokit as Core } from "@octokit/core";
import { requestLog } from "@octokit/plugin-request-log";
import { paginateRest } from "@octokit/plugin-paginate-rest";
import { restEndpointMethods } from "@octokit/plugin-rest-endpoint-methods";
import { VERSION } from "./version";
export const Octokit = Core.plugin(requestLog, restEndpointMethods, paginateRest).defaults({
    userAgent: `octokit-rest.js/${VERSION}`,
});

However, this setup leads to an error in the chrome debugger:

Uncaught TypeError: Failed to resolve module specifier "@octokit/core". Relative references must start with either "/", "./", or "../".

Although I prefer not to change the @octokit/ reference to '../../' to avoid manual adjustments during the npm push task executed by my gulpfile.js, I modified the index.js temporarily for debugging purposes:

import { Octokit as Core } from "../../core";
import { requestLog } from "../../plugin-request-log";
import { paginateRest } from "../../plugin-paginate-rest";
import { restEndpointMethods } from "../../plugin-rest-endpoint-methods";
import { VERSION } from "./version";
export const Octokit = Core.plugin(requestLog, restEndpointMethods, paginateRest).defaults({
    userAgent: `octokit-rest.js/${VERSION}`,
});

This modification led to similar error messages for each import, referencing non-specific files:

index.js:4 GET https://localhost:44364/lib/@octokit/plugin-rest-endpoint-methods net::ERR_ABORTED 404

The above URL points to the directory rather than a particular file. Interestingly, accessing a single file using the directory path directly loads the file in the browser without issues. For example:

https://localhost:44364/lib/@octokit/plugin-rest-endpoint-methods/dist-src/endpoints-to-methods.js

shows the JS file correctly. My goal is to utilize this package in custom js code that generates cards displaying repo information, like so:

var octokit = new Octokit({ userAgent: 'agentName' });

Unfortunately, the above code throws errors related to the existence of Octokit.

Hence, my question remains - what could be causing this issue? Any insights on where I should focus my research effort would be highly appreciated.

It's likely that my struggles stem more from my lack of understanding in properly importing JavaScript libraries into my asp .net core solution rather than any faults in the octokit package itself.

Answer №1

Struggling with incorporating Octokit? Here are a few key challenges you may be facing: handling the @ symbol, importing within the correct scope, and attempting to utilize files designed for build tools.

@ in a Razor Page

When embedding JavaScript inline in a <script> tag within a Razor page, remember to escape the @ character by using @@. For example, you would write @@octokit/rest when referencing the Octokit path.

Scope

Using type=module establishes module scope, limiting access to the Octokit variable outside of the module. To extend its accessibility, attach the variable to the window object.

window.Octokit = new Octokit({ userAgent: 'agentName' });

Afterward, other script blocks can interact with Octokit normally:

const { data } = await Octokit.request("/user");

Building Octokit

The imported files are not meant for direct browser utilization. Instead, they are tailored for JavaScript build tools like Webpack. Adjust your workflow by incorporating the necessary plugins for gulp or webpack to process and output files usable by browsers.

Consider sourcing Octokit from their CDN as an alternative solution to streamline the loading process without manual build tool configurations.


In the future, browsers may support import-maps, potentially simplifying package resolution directly through the browser. This could significantly enhance efficiency when working with libraries like Octokit on Razor pages.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting the Owl carousel's responsiveness with a div width of 1170px

Whenever my display size is less than 1170px, the width of the owl carousel div overflows. How can I fix this issue? jQuery(document).ready(function($) { "use strict"; // CUSTOMERS TESTIMONIALS CAROUSEL $('#customers-testimonials&a ...

Insert the content from the th element into a custom data attribute within the corresponding td cell in that specific column

To achieve dynamic content insertion, the text within each <th> tag must be stored in a variable and assigned to the data-th attribute of the respective <td> element. This assignment should follow the order of appearance in the table structure. ...

How can I create a button that prints the page content and then sends it to the backend using JavaScript for submission?

Is there a way to print content from a page and then submit form data to make changes to a backed database using just one button? I've attempted to include the print action within an on-click function, but it does not submit the form after printing. ...

Steps for executing .aspx files on IIS in Windows XP SP2

Setting up an ASP.Net Server (IIS) on my Desktop to run my asp.net .aspx pages has been a bit challenging. Currently, I am using Windows XP Service Pack 2. To install IIS, I started by inserting the XP CD into the disk drive and then navigating to Control ...

NodeJS hit with ECONNREFUSED error while trying to run localhost server

I currently have a NodeJS server running on my local machine, listening to port 50000. I am trying to make a simple GET request to this server from another local server, but I keep receiving an ECONNREFUSED error message: { Error: connect ECONNREFUSED 127 ...

Pass a notification to a separate function

Issue Looking for a way to send an 'event' to another function using jQuery. The goal is to prevent the removal of a table row before executing certain treatments, and then remove the row. I want to insert a modal window in between these actions ...

When refreshed using AJAX, all dataTable pages merge into a single unified page

I followed the instructions on this page: How to update an HTML table content without refreshing the page? After implementing it, I encountered an issue where the Client-Side dataTable gets destroyed upon refreshing. When I say destroyed, all the data ...

Guide to incorporating a scroll-follow effect in multiple directions

I am facing a challenge with managing an array of divs that exceed the dimensions of their container. I have set overflow to hidden on the container and used JQuery Overscroll to achieve an iPhone-like scrolling effect on the map. The problem I'm try ...

ES6 syntax makes use of variables enclosed in curly braces

Could you explain the impact of the bracket followed by a braces combination (for example, ({user}) below)? promise1.then(user => ({user})); ...

The sidebar momentarily shrinks before expanding again when the page is loaded

There is a sidebar on my website that can expand or collapse when a button is clicked. I have successfully saved its state in the localStorage, but there is a small issue that I need help with. When the page loads and there is no saved state in the localS ...

Using more than one request URL mapping handlers in a single controller does not function properly within Node's Express JS framework

Within my server.js file, I have set up the following URL mappings: app.use('/registerCourse', require('./controllers/training.controller')); app.use('/getCourses', require('./controllers/training.controller')); In ...

Exploring the optimal approach for distinguishing between numbers and strings in a JavaScript/Typescript class

I recently encountered a situation with my Typescript/React solution where I defined a property as a number and set the input type to "number", but when the state value was placed in an input field, it would change to a string unless properly handled. In ...

"Utilizing NodeJs with Mongoose for handling promises using the then/c

Encountered an issue with Mongoose promises MyModel.find().then((data)=> Promise.reject()) .catch(()=>console.log('first catch')) .then(()=>console.log('ok')) .catch(()=>console.log('second catch&a ...

The package.json file for system-wide installation of packages

Trying to perform an update on my nuclide-server has been a challenge. The command sudo npm update -g nuclide --verbose returned the following output: $ sudo npm update -g --dev nuclide --verbose npm info it worked if it ends with ok npm verb cli [ ' ...

troubles arise when using undeclared functions in javascript

Recently, I've been working on implementing a Javascript CountDown Timer triggered by a button click. The Javascript code is stored in an external file, as well as the CSS for styling the timer. The problem arose when I tried including the Javascript ...

Is there a way to showcase a single HTML canvas across multiple div elements?

I'm currently developing a web application that includes a Google Maps feature in two different tabs using Twitter Bootstrap. I am exploring options to have one canvas element that can be displayed in both tabs with varying dimensions. One idea is to ...

Sending an array to another file upon button click event in a React application

Hey everyone, I'm currently getting started with React. I have this interesting situation where I need to handle an array of IDs that are obtained from selected checkboxes. My goal is to pass this array to another file called Employee.js when a button ...

Adjust the map automatically as the cursor approaches the map's edge in Google Maps API V3

My latest project involved creating a selection tool using the Rectangle shape tool. With this tool, users can easily select markers by drawing a rectangle over them and releasing their mouse to erase the selection (similar to selecting items on a desktop ...

Improving Load Times in Next.js

I've made an interesting observation: when I execute the npm run dev command to start the code, the initial page load takes much longer compared to subsequent page refreshes. Upon inspecting the network tab in Chrome DevTools, I discovered that the f ...

Different Slide Library Fullpage.js

Is it feasible to have two sections with the same slide in fullpage.js? For example, if I'm currently on slide 1 of section 1 and then move to slide 2 of section 1, can section 2 automatically switch to slide 2 as well? I'm curious if this funct ...