question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'import' and 'export' may only appear at the top level

See original GitHub issue
Module parse failed: /path/to/project/node_modules/imports-loader/index.js?this=>window!/path/to/project/src/entry.js 'import' and 'export' may only appear at the top level (4:0)
You may need an appropriate loader to handle this file type.
| (function() {
|
| import './NameSpace';
| import './Polyfill';
    rules: [
      {
        test: regx,
        use: [
          {
            loader: "imports-loader?this=>window",
          },
        ],
      },
    ],

But import 'imports-loader?this=>window./NameSpace'; this works fine.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
SteinNscommented, Oct 31, 2017
import _ from 'lodash';
import { cube } from './math';
import Print from './print'

if(process.env.NODE_ENV !== 'production') {
	console.log('development');
}
// import './style.css';
function component (  ) {
	var elm = document.createElement('div');
	var btn = document.createElement('button');
	var br = document.createElement('br');

	elm.innerHTML = join(['hel1lo','webpack'], ' ');
	// elm.innerHTML = ['hello webpac1k!'].join(' ');
	btn.innerHTML = 'click print';
	this.alert('Hmmm, this probably isn\'t a great idea...');
	elm.appendChild(br);
	elm.appendChild(btn);
	btn.onclick = Print.bind(null, 'hello')
	return elm;
}
document.body.appendChild(component())
const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');

module.exports = {
	entry: {
		app: './src/index.js',
		vendor: ['lodash']
		another: './src/another-module.js'
	},
	module: {
		rules: [
			{
				test: require.resolve('./src/index.js'),
				use: 'imports-loader?this=>window'
			},
			{
				test: /\.css$/,
				use: [
					'style-loader',
					'css-loader'
				]
			},
			{
				test: /\.(png|svg|jpg|gif)$/,
				use: [
					'file-loader'
				]
			},
			{
				test: /\.(woff|woff2|eot|ttf|otf)$/,
				use:[
					'file-loader'
				]
			},
			{
				test: /\.(csv|tsv)$/,
				use: [
					'csv-loader'
				]
			},
			{
				test: /\.xml$/,
				use: [
					'xml-loader'
				]
			},

		]
	},
	plugins: [
		new CleanWebpackPlugin(['dist']),
		new HtmlWebpackPlugin({
			title: 'production'
		}),
		new webpack.optimize.CommonsChunkPlugin({
			name: 'vendor'
		}),

		new webpack.optimize.CommonsChunkPlugin({
			name: 'runtime'
		}),
		new webpack.ProvidePlugin({
			join: ['lodash', 'join']
		})

	],
	output: {
		filename: '[name].[hash].bundle.js',
		path: path.resolve(__dirname, 'dist'),
		chunkFilename: '[name].bundle.js'
	}
}

Hi, I’m a webpack beginner. I followed the webpack official guide using imports-loader and found an error

ERROR in ./src/index.js
Module parse failed: 'import' and 'export' may only appear at the top level (7:0)
You may need an appropriate loader to handle this file type.
|  * Created by Administrator on 2017/10/23.
|  */
| import _ from 'lodash';
| import { cube } from './math';
| import Print from './print'
 @ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/index.js

6reactions
michaelzhngcommented, Nov 26, 2018

@SteinNs I had the same issue then I found that imports-loader just does this to my module:

/* src/index.js */
(function () {
  // my module
}.call(window));

If there is an import statement in the module, webpack will throw an error to the terminal because the statement is in a function, not at the top level. You can use CommonJS require function instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

'import' and 'export' may only appear at the top level
I'm using webpack with vuejs. Webpack does its thing, but when I look at the outputted app.js file, it gives me this error....
Read more >
'import' and 'export' may only appear at the top level · Issue #304
Error: 'import' and 'export' may only appear at the top level abc: ../../node_modules/lodash-es/isBuffer.js (1:0) abc: 1: import root from ' ...
Read more >
'import' and 'export' may only appear at the top level | bobbyhadz
The error "import and export may only appear at the top level" occurs when we forget a closing brace when declaring a function...
Read more >
Import and export may only appear at the top level
The error "import and export may only appear at the top level" may occur due to a missing closing brace when declaring a...
Read more >
How to fix: import and export may only appear at the top level
So I've setup a new Svelte project and install all the latest node modules. ... If you're developing a Svelte project, you're probably...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found