Error: export keyword used incorrectly

Currently, I am in the process of developing an npm package called foobar locally. This allows me to make real-time changes and modifications without the need to constantly publish and unpublish the package, which greatly improves my development efficiency and sanity.

In my projectTest, I have established a link to foobar using the command npm link foobar. For those unfamiliar, the npm link flag creates a symbolic link to global modules (for more information see: https://docs.npmjs.com/cli/link)

The projectTest is actually a ReactJS project written in ES2015 with tools like Webpack and babel. Here, I can import { x } from 'package' without encountering any issues.

However, even though the package foobar exists in the node_modules directory with a link to its development folder, when I attempt to import:

import { myFn } from 'foobar'

I encounter the following error in the console:

/foobar/lib/index.js:3
export const loadImage = () => {
^^^^^^
SyntaxError: Unexpected token export
    at Object.exports.runInThisContext (vm.js:78:16)
    at Module._compile (module.js:543:28)

This issue surprises me since other imports are functioning perfectly fine with ES2015 code. These imports are not transpiled beforehand or saved as static files, but rather executed in real time thanks to my webpack setup with babel loader configured as follows:

... (webpack config example provided)

To troubleshoot this problem, I experimented with importing a module using the full or relative path instead of the linked package from node_modules. For instance:

import { myFn } from '/bar/zoo/lar/deep/foobar'

In this scenario, where the syntax/file mirrors working imports for utility functions across the project, the same unexpected token export error occurs.

User @felixKling suggested that webpack might be ignoring the node_module, leading to the issue persisting even after removing it:

... (error snippet shown)

Upon further investigation, it seems that only code within the specified context in the webpack dev configuration file gets transpiled during runtime. However, changing the syntax resolves the issue, indicating that it is related to how babel-loader operates within the context.

... (another webpack configuration snippet)

Even though this workaround works seamlessly, the root cause behind the syntax error remains unclear. Additionally, confirming that the syntax modification fixes the issue suggests a connection to how babel-loader functions within the specified context.

module.exports = {
  myFn: function () {
    console.log('Syntax correct! myFn works!')
  }
}

For reference, I am utilizing webpack2 and node v7.4.0 for this project.

Answer №1

It appears that the issue lies in the foobar project not being transpiled when connected via npm link. However, projectTest should not need to compile the foobar project; its purpose is simply to link to it. To address this issue, you will need to transpile the foobar project using babel before linking it to another project. One way to achieve this is by adding a build script to the package.json file of the foobar project, which will transpile the code into a /dist folder. You can then modify the main field of the package.json file as follows:

"main": "./dist/index.js"

Include the command npm run build in your development workflow to ensure that the dist folder stays updated while working on the foobar project. This should enable projectTest to successfully link to the transpiled version of foobar.

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

Multiple requests are being sent via AJAX

Despite numerous other posts addressing the same issue, I have not been able to find a solution for my problem. The AJAX request appears to be sent multiple times. var checkAllStatus = function () { $.ajax({ cache: false, type: "POST", ...

Tips for ensuring only one dropdown is opened at a time

Hey there! I've been struggling with getting my dropdowns to open one at a time on my website. I attempted to hide them all by default, but they still insist on opening simultaneously. Any ideas on what I might be overlooking? Thank you for your help! ...

Karma and RequireJS fetching files beyond the base URL

My goal is to set up automatic testing using karma with the following file structure: /assests /css /font /img /js /collection /lib /model /plugin /spec /view test-main.js main.js /templates index.html karma.conf.js In my ...

The VueRouter is unresponsive and not functioning as expected

I have been delving into Vue. Through the npm install vue-router command, I added vue-router to my project. Subsequently, I incorporated VueRouter and defined my URL paths within the VueRouter instances located in the main.js file. I created an About compo ...

Automatic Refresh and Search Functionality on PHP Page

Can anyone help with a problem I'm having trying to implement auto-reload on a page with a table in the form of rows that contain usernames? I am currently using the jQuery searchable plugin from here to search for usernames within the div. The websi ...

Move the option from one box to another in jQuery and retain its value

Hey guys, I need some assistance with a jQuery function. The first set of boxes works perfectly with the left and right buttons, but the second set is not functioning properly and doesn't display its price value. I want to fix it so that when I click ...

What is the method for identifying the environment within an Express.js application?

Is there a reliable method for determining the environment in which an expressJS app is currently operating (development, test, production)? I have checked process.env, but found no clear indication of the environment. I know that variables can be set in ...

What causes the inconsistency in TypeScript's structure typing?

It is well-known that TypeScript applies structure typing, as demonstrated in the following example: interface Vector { x: number; y: number; } interface NamedVector { x: number; y: number; name: string; } function calculateLength(v: Vecto ...

Guide to dynamically loading customer data into an HTML table using JavaScript

I'm looking to populate a table with data on customers including their name, customer ID, and rental cost. Can anyone help me with the JavaScript code needed to insert this data into rows of the table? Your assistance is greatly appreciated. Below is ...

Refreshing data causes the data to accumulate and duplicate using the .append function in Jquery

I am currently working with a dynamic table that is being populated using AJAX and jQuery. Everything seems to be functioning correctly, however, I am encountering an issue when trying to reload the data as it just continues to stack up. Below is the func ...

Is react-scripts missing from my create-react-app project on Ubuntu?

I have been working with ReactJS and using the following: https://github.com/facebookincubator/create-react-app However, when I try to deploy it on DigitalOcean using Ubuntu 16, I am encountering an issue where running npm run build results in an error me ...

View the picture directly on this page

Currently, I am in the process of creating a gallery and I would like the images to open on top of everything in their original size when clicked by the user. My expertise lies in HTML and CSS at the moment, but I am open to learning JavaScript and jQuery ...

Understanding Pass by Reference within Objects through Extend in Javascript with underscore.js Library

When working with Javascript and using the extend function in the underscore.js library, I am curious about what happens in relation to memory. Consider this example: var obj = {hello: [2]}; var obj2 = {hola: [4]}; _.extend(obj, obj2) obj2.hola = 5; conso ...

Encountering an issue with Material-UI and Next.js: "TypeError: theme.spacing is not a function

Encountering an issue after modifying _app.js to dynamically generate a material UI theme. I've been following the implementation example provided by the material-ui team at: https://github.com/mui-org/material-ui/tree/master/examples/nextjs. To summ ...

Unable to replicate the functionality of the tab key using jQuery for the enter key

How can I focus on the first input ('Qtd on the table') after pressing enter on the 'Buscar' input? I've tried various solutions like $(this).nextAll('input').first().focus(); $(this).next('input:text').focus ...

What is the best way to create a line break in a flex div container to ensure that overflowing items wrap onto the next line using

Using material-ui popper to display a list of avatars. Trying to arrange the avatars horizontally within the popper. <Popper style={{ display: 'flex', maxWidth: '200px', }}> <div style={{ marginRight: '20px' }}&g ...

Remove the image by clicking on the "X" icon located on the top right corner of the image

One of my tasks involves deleting an image by clicking on the "X" mark located at the top right corner of the image. To achieve this, I referred to this CSS fiddle http://jsfiddle.net/yHNEv/. Sample HTML code: <div class="img-wrap"> <span ng-c ...

The issue with ng-if not functioning within ng-repeat is that the ng-if directive

My issue is with using ng-if inside an ng-repeat in AngularJS. Despite updating to the latest version on 09/27/2014, I am still unable to make it work properly. The code functions perfectly outside of ng-repeat, and also works fine inside ng-repeat when us ...

Setting up Scss and purgeCss configuration in Next.js custom postCSS configuration: A step-by-step guide

My current project is using Scss in combination with Bootstrap for design. I have implemented purgeCss to remove unused Css, and customized my postcss.config.js file as follows: module.exports = { plugins: [ "postcss-flexbugs-fixes", [ " ...

PHP is not receiving any data from the Ajax request

I'm currently attempting to set up my first Ajax example on my MAMP server. Here's how my ajax.html file looks: <html> <head> <script src='ajax.js'></script> </head> <body onload = 'ajax()'> ...