What steps should I take to troubleshoot the ParseError related to the restriction of using 'import' and 'export' exclusively with 'sourceType: module' for importing UpgradeAdapter?

I have been working on upgrading an angular.js app to angular 2, following the guidelines provided at https://angular.io/docs/ts/latest/guide/upgrade.html.

The application is already coded in Typescript, and we are using browserify and tsify for compiling and bundling the app.

However, when I tried to bootstrap the hybrid app after installing angular 2 dependencies via npm, browserify threw the following error:

/my-project/node_modules/@angular/upgrade/static.js:8
export { downgradeComponent } from './src/aot/downgrade_component';
^
ParseError: 'import' and 'export' may appear only with 'sourceType: module'

This error occurred after adding the following code snippet:

import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app.module';
import {UpgradeModule} from '@angular/upgrade/static';

platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
    const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
    upgrade.bootstrap(document.documentElement, ['sagaDesktopApp']);
});

Here is the content of my tsconfig.json:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false,
        "suppressImplicitAnyIndexErrors": true
    }
}

I would greatly appreciate any assistance with this issue.

Answer №1

The issue has been successfully replicated. It appears to be stemming from the presence of a static.js file within the @angular/upgrade module. When Browserify tries to resolve @angular/upgrade/static, it erroneously resolves to:

node_modules/@angular/upgrade/static.js

However, the correct resolution should be:

node_modules/@angular/upgrade/static/package.json

Within the package.json located in the static directory, there is a main entry crucial for compatibility with non-ES6 bundlers like Browserify.

Browserify follows a similar module resolution mechanism as Node.js and should prioritize checking for the file over the directory. To resolve this issue, it is advised to append a trailing slash to the import:

import { UpgradeModule } from '@angular/upgrade/static/';

By adding the trailing slash, Browserify will correctly prioritize the directory and resolve the package.json during bundling.

For further insights, additional information regarding the significance of main/module entries in the package.json file can be found in this Tsify issue.

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

Error: unable to locate module that was imported from

Every time I try to run the project using this command, an error pops up: > npm run build npm info using <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7a9b7aa87fee9f1e9f0">[email protected]</a> npm info using ...

Using a static enum in a different class in TypeScript: A guide

After referencing this question and answer on Stack Overflow about setting a static enum inside a TypeScript class, I decided to create my own enum and implement it as a static property in my class. Here is how I did it: /* Input.ts */ enum INPUT_TYPE { T ...

The conversion of a newline in an Angular page is done using &lt;br/&gt tag

Here is a function I have: setLocalVariableOnAccepted(ogp, hb, responseJson) { if (responseJson.ofgp === undefined) { this.ogpStatus = 'orange'; this.ogpStatusMsg = responseJson.ofgp + ', <br/> No change. Previous va ...

Issue with Angular2: Modifying onClick property does not cause view to refresh

For my latest project, I am attempting to replicate the functionality of the TodoMvc app using Angular2. One major hurdle I'm currently facing is implementing filtering for the list based on a click event. You can check out my progress on Codesandbox ...

Utilize the data structures and variables from one module to enhance the functionality

Currently, I am utilizing Babylonjs with Rollupjs in conjunction with typescript. https://i.sstatic.net/2L3bY.png When importing Babylonjs like so: import { ArcRotateCamera, Engine, SceneLoader, Vector3 } from "babylonjs"; I am able to access all the t ...

Identifying the category of a value through a conditional check on another value

I am looking for my code editor to automatically determine the type of extraData based on the value of error, which is being narrowed down by an if statement: export enum ErrorCodes { Unknown = 'UNKWN', BadRequest = 'BDREQ', } int ...

Using onDoubleClick with MUI TextField: A Quick Guide

Whenever the user double clicks the input field, I would like to automatically select the text. I have created a function for this specific action: export const selectText = ( event: React.MouseEvent<HTMLInputElement | HTMLTextAreaElement, MouseEvent& ...

Complex calculations that require iterative processing to yield various results periodically

Currently, I am facing a challenge with my controller in processing requests that require extensive computing. The computations take time and generate multiple values at different stages, which need to be transmitted to the frontend as they are calculated. ...

Issue with angularjs ui.router delaying the rendering of ui-view

I've been working on implementing ui-router in my project. Here is the core module setup: var core = angular.module('muhamo.core', ['angular-loading-bar', 'anguFixedHeaderTable', 'ui.router']); For the tra ...

Component not appearing in Storybook during rendering

I'm trying to incorporate the MUI Button Component into my storybook and then dynamically change MUI attributes like variant, color, and disabled status directly from the storybook. While I was successful in doing this with a simple plain HTML button, ...

Ionic 2 - Error: Module ""."" not found at runtime

Encountered a perplexing error while running my Ionic 2 application on localhost using the command: ionic serve I've diligently inspected all my imports for any incorrect paths in my TypeScript files, but haven't found anything amiss. The only ...

Displaying summaries for all items (including those that are not currently visible) in the Kendo UI Grid while paging or grouping

Is it possible to display aggregates for all items in a grid data source while using paging and grouping? In my list of 1000+ items with columns like Description, Type, Cost, I am fetching only 200 items at a time using OData. However, when viewing the ag ...

ng-options dependent on the value selected previously

Below is an array that I'm working with. $scope.set = [ {"name":"name1", "value":"value1"} {"name":"name2", "value":"value2"} {"name":"name3", "value":"value3"} ] I want to display options based on previous selections. For example, if I choose name1 ...

Achieve consistent action execution for both the footer and header included in all pages using AngularJS

This particular query has a significant possibility of being considered a duplicate, but in my attempts to find a satisfactory explanation for my problem through various searches, I have not been successful - so please accept my apologies if this is indeed ...

Increasing response buffer size in Node.js fetch for version 2.x.x

Currently in the process of implementing an API request using nodejs-fetch and I've encountered an issue. The documentation states that the maximum buffer size for fetch is 16kB, but the response I need to retrieve is 53 kB. This causes the .fetch() f ...

What is the expected return type in TypeScript of a function that returns a void function?

I recently received feedback during a code review suggesting that I add return type values to my functions. However, I am unsure of what return type to assign to this particular function: function mysteryTypeFunction(): mysteryType { return function() ...

Why won't my timer directive properly update the View as expected?

Experimenting with directives and trying to update the DOM from the directive. The link function in the directive is not able to see the scope controller vars. If you can help me understand what I'm doing wrong, that would be greatly appreciated! I& ...

Exploring the world of HTTP PUT requests in Angular 4.0

I have encountered an issue with a function I wrote for sending an http put request to update data. The function is not receiving any data: updateHuman(human: Human) { const url = `${this.url}/${human.id}`; const data = JSON.stringify(human); ...

TypeScript does not properly validate the types of defaultProps

When working with TypeScript 3.0, I consulted the documentation at https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html The recommendation is to use static defaultProps: Pick<Props, "name"> as an explicit type annotation ...

Incorporating a Symbol into a Function

Reviewing the following code snippet: namespace Add { type AddType = { (x: number, y: number): number; }; const add: AddType = (x: number, y: number) => { return x + y; }; } Can a 'unique symbol' be added to the AddType lik ...