Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/How to fix parseerror: 'import' and 'export' may appear only with 'sourcetype: module'?
Next
Answered
Daryl Schmidt
  • 3
Daryl Schmidt
Asked: July 23, 20222022-07-23T10:29:23+00:00 2022-07-23T10:29:23+00:00In: Error

How to fix parseerror: ‘import’ and ‘export’ may appear only with ‘sourcetype: module’?

  • 3

. Advertisement .

..3..

. Advertisement .

..4..

I have just got parseerror: ‘import’ and ‘export’ may appear only with ‘sourcetype: module’ error. My detail error below:

PS D:\Work\dev2qa.com-example-code\PythonExampleProject\html\browserify-example> browserify ./js/test1.js -o build.js

D:\Work\dev2qa.com-example-code\PythonExampleProject\html\browserify-example\js\test1.js:1
import helloFromTest2, { moduleName } from "./test2.js";
^
ParseError: 'import' and 'export' may appear only with 'sourceType: module'

Please help me find out the way to solve that problem. I am very grateful for your help. Thanks so much.

 

  • 1 1 Answer
  • 420 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-07-23T11:13:48+00:00Added an answer on July 23, 2022 at 11:13 am

    The cause: This issue occurs because the test1.js file’s javascript source code syntax is not supported by Browserify. The JS code may be written in ES6+ style. Solution: Therefore, you ought to use Babel to transcompile the javascript code to a lower syntax that Browserify accepts, like es2015. It can fix the error parseerror: ‘import’ and ‘export’ may appear only with ‘sourcetype: module’.

    1. Set up the Babel Modules. – To generate a package.json file in the current folder, open a terminal and execute the command npm init. – To install the plugin modules, execute the command npm install –save-dev @babel/core @babel/cli @babel/preset-env in the terminal. – To verify the module installation, use the commands npm list @babel/core, npm list @babel/cli, and npm list @babel/preset-env. – The aforementioned babel plugin modules are also included in the project’s package.json file like the following:
      {
      "name": "browserify",
      "version": "1.0.0",
      "main": "index.js",
      "scripts": {
      "test": "echo "Error: no test specified" && exit 1"
      },
      "author": "",
      "license": "ISC",
      "description": "",
      "devDependencies": {
      "@babel/cli": "^7.17.10",
      "@babel/core": "^7.18.2",
      "@babel/preset-env": "^7.18.2"
      }
      }
    2. Make a Babel configuration file. – Make a babel.config.json configuration file in the project directory. – The file babel.config.json should now contain the JSON source code below.
      {
      "presets": [
      [
      "@babel/preset-env",
      {
      /*
      Define the targets web browsers.
      You can ignore this settings for all web browsers.
      
      "targets": {
      "edge": "17",
      "firefox": "60",
      "chrome": "67",
      "safari": "11.1"
      },
      */
      "useBuiltIns": "usage",
      "corejs": "3.6.5"
      }
      ]
      ]
      }
    3. Transcompile different JavaScript source files into one by using the Babel command. – Execute the command./node modules/.bin/babel js -out-dir lib in the project folder from the terminal. – The aforementioned command will transcompile all of the javascript files in the js folder to the lib folder and create a lib folder in the current project folder.
      ├─js
      │  ├─test1.js
      │  └─test2.js
      ├─lib
      │  ├─test1.js
      │  └─test2.js

      – The javascript source code for the files lib/test1.js and lib/test2.js may be seen on an earlier web browser by opening them in a text editor. 

    • 3
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.