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

Suggestions for relocating this function call from my HTML code

I have been working on updating a javascript function that currently uses an inline event handler to a more modern approach. My goal is to eliminate the unattractive event handler from the actual HTML code and instead place it in a separate modular javascr ...

Crockford's system for safeguarded entities

Despite the abundance of questions and resources related to "Javascript: The Good Parts," I am struggling to comprehend a specific sentence in the book. On pages 41-42, the author defines the serial_maker function as follows: var serial_maker = function ( ...

Leveraging the power of axios.all for dynamic operations

Is there a way to efficiently reuse the same Vue-component for both editing and creating new users? While working with vue-router, I have implemented a beforeRouteEnter method to fetch data via API. Depending on whether an ID is set in the URL parameter, ...

I can't figure out why this form isn't triggering the JS function. I'm attempting to create an autocomplete form field that connects to a MySQL database using a PHP script and AJAX

I am encountering an issue while trying to implement the .autocomplete() function from jQuery UI with a list of usernames fetched from a MySQL database using a PHP script. Strangely, it is not functioning as expected and no errors are being displayed in th ...

Tips for enhancing a JSON array by including attributes through JavaScripts

I need help in dynamically constructing JSON using Javascript. { "Events": [{ "Name": "Code Change", "Enabled": "true", "Properties": [{ "Name": "url", "Value": "val" }] }], "Properti ...

Executing a javascript function from an ajax-loaded webpage

I have a form where I upload a file using an ajax call that includes an API request. Once the API call is successful, I need to update a table displaying the list of uploaded files. Initially, I attempted calling a JavaScript function within the ajax page, ...

Error: Unable to find the definition for Image (Next.js)

This new component in my next js project allows me to write a quote on an image and display it on the canvas. However, I am encountering an issue with the Image() function in JavaScript which typically runs on the browser. It seems that Next.js first execu ...

"Looking to trigger a server click using JQuery or Javascript in your code? Here's how you can

I am facing an issue with triggering a Server-Side OnClick event on an ASP Server Button within a User Control using JavaScript or JQuery. The current methods I have tried do not produce the desired result as they do not actually simulate a user clicking t ...

Adjusting a Vue Slider with a changing value

Currently, I am utilizing the Vue Slider component from this source. My goal is to enhance the functionality of the slider by increasing it to the next index and beyond. The issue lies in the fact that the current implementation increments by 1, as the val ...

Is there a specific code repository available that can verify and transform blog comments into XHTML strict format?

I'm currently developing a PHP website that allows users to make comments similar to a blog, and I am specifically interested in restricting the use of certain HTML tags. Is there any existing library that can process user comments and output valid XH ...

Can the console logs be disabled in "Fast Refresh" in NextJS?

When I'm running multiple tests, my browser's console gets cluttered with messages every time a file is saved. Is there a way to disable this feature? For example: [Fast Refresh] completed in 93ms hot-dev-client.js?1600:159 [Fast Refresh] rebuil ...

Limit the number of browser tabs in which users can access the Web Application Options Link

Our online B2B application, developed using ASP.NET webforms on .Net framework 4.7.2, needs to limit users to opening only 3 to 4 tabs in their browser simultaneously. This restriction is necessary to manage server load due to a large number of active user ...

Encountering an error with requireJS: "Unknown provider $routeProvider with AngularJS 1.2.9 ngRoute"

Currently, I am utilizing angularJS-1.2.9 and angular-route-1.2.9 to configure routes for my application. Additionally, I have integrated requireJS as the dependency loader to modularize the code. Despite adding the ngRoute dependency into the AngularJS co ...

What could be the reason for a jQuery script failing to execute when included in a PHP include statement from a different PHP page?

I'm currently working with javascript and php to manage cookies across different pages. On one page, I have a script that sets a value in a cookie variable and I want to retrieve that value on another page. Let's say the first page is named page1 ...

Is there a way to prevent advertisements from appearing in npm?

As I execute various npm commands, my console gets bombarded with ads promoting different projects and individuals. While I enjoy contributing to open source projects, I believe the console output of a tool should not be used for advertising. Thank you fo ...

When located at the bottom of a page, the Div element fails to display

I need some help with my javascript code. I am working on creating a scrollable panel that closes when scrolled and reveals a #toTop div when the bottom of the page is reached. Below is the function code I have written, but for some reason, the #toTop div ...

What is the best method for choosing these elements and surrounding them with a wrapper?

I need to style a title with two radio inputs by wrapping them in a form: <p><strong>Country</strong></p> <div class="radioWrapper"> <span class="label">Canada</span> <span class="radio"> ...

When utilizing a clone in a Draggable and Droppable scenario, the data/attribute is obtained from the

Currently, I am struggling with retrieving data/attributes from the original item when using jQueryUI draggable/droppable to move rows between tables. I have set helper: 'clone' but am finding it difficult to access any data from the dragged item ...

Setting up Quasar plugins without using Quasar CLI: A step-by-step guide

I integrated Quasar into my existing Vue CLI project by running vue add quasar. Now I'm attempting to utilize the Loading plugin, but unfortunately, it's not functioning as expected. Here is the configuration setup for Quasar/Vue that I have in ...

Encounter an error when reading a stream with a maximum timeout value set

I have encountered a challenge while trying to read and process large CSV files. Due to a rate limit in processing, I need to introduce a 1-minute delay between each request. Initially, I attempted to use set timeout, but discovered that there is a limitat ...