I am facing the
ERROR in ./index.js 1:0
Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (1:0)
File was processed with these loaders:
* ./node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these
loaders.
> import { startServer } from "./server";
| import _ from 'lodash';
While executing the npx webpack
for creating build for NodeJS application. This is my webpack.config.js
and package.json
file.
webpack.config.js
const path = require('path');
module.exports = {
mode: 'production',
target: 'node',
entry: './index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env']
],
},
},
},
],
},
};
package.json
{
"name": "restapi-ts",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "monjs",
"scripts": {
"pile": "tsc",
"start": "npm run pile && node ./dist/index.js",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"lodash": "^4.17.21",
"pg": "^8.10.0",
"pgtools": "^0.1.1"
},
"devDependencies": {
"@babel/cli": "^7.21.0",
"@babel/core": "^7.21.4",
"@babel/node": "^7.20.7",
"@babel/preset-env": "^7.21.4",
"@types/express": "^4.17.17",
"@types/pg": "^8.6.6",
"babel-loader": "^9.1.2",
"typescript": "^4.9.5",
"webpack": "^5.77.0",
"webpack-cli": "^5.0.1"
}
}
server.ts
import express from 'express';
import Route from './src/routes'
const bodyParser = require('body-parser');
const app = express();
// Parse JSON data
app.use(bodyParser.json());
//app.use(express.json());
app.get("/", (req, res) => {
res.send("Hi World");
});
app.use("/api/v1/order", Route);
// Start the server
export function startServer()
{
app.listen(3000, () => {
console.log('Server started on port 3000');
});
}
index.js
import { startServer } from "./server";
//const { startServer } = require("./server");
import _ from 'lodash';
startServer();
I have downloaded all the webpack dependencies and have the babel loader dependencies also. Please share your suggestion how this error can be fix.
I am facing the
ERROR in ./index.js 1:0
Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (1:0)
File was processed with these loaders:
* ./node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these
loaders.
> import { startServer } from "./server";
| import _ from 'lodash';
While executing the npx webpack
for creating build for NodeJS application. This is my webpack.config.js
and package.json
file.
webpack.config.js
const path = require('path');
module.exports = {
mode: 'production',
target: 'node',
entry: './index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env']
],
},
},
},
],
},
};
package.json
{
"name": "restapi-ts",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "monjs",
"scripts": {
"pile": "tsc",
"start": "npm run pile && node ./dist/index.js",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"lodash": "^4.17.21",
"pg": "^8.10.0",
"pgtools": "^0.1.1"
},
"devDependencies": {
"@babel/cli": "^7.21.0",
"@babel/core": "^7.21.4",
"@babel/node": "^7.20.7",
"@babel/preset-env": "^7.21.4",
"@types/express": "^4.17.17",
"@types/pg": "^8.6.6",
"babel-loader": "^9.1.2",
"typescript": "^4.9.5",
"webpack": "^5.77.0",
"webpack-cli": "^5.0.1"
}
}
server.ts
import express from 'express';
import Route from './src/routes'
const bodyParser = require('body-parser');
const app = express();
// Parse JSON data
app.use(bodyParser.json());
//app.use(express.json());
app.get("/", (req, res) => {
res.send("Hi World");
});
app.use("/api/v1/order", Route);
// Start the server
export function startServer()
{
app.listen(3000, () => {
console.log('Server started on port 3000');
});
}
index.js
import { startServer } from "./server";
//const { startServer } = require("./server");
import _ from 'lodash';
startServer();
I have downloaded all the webpack dependencies and have the babel loader dependencies also. Please share your suggestion how this error can be fix.
Share Improve this question edited Apr 5, 2023 at 14:12 Jerryh001 9723 silver badges12 bronze badges asked Apr 5, 2023 at 13:42 rishu rathaurrishu rathaur 611 gold badge1 silver badge5 bronze badges 1- did you ever fix this? i'm having the same thing with trying to use React + Webpack + Typescript + Cesium – MaylorTaylor Commented Aug 8, 2023 at 19:16
2 Answers
Reset to default 11If you have "type": "monjs",
in your package.json, remove it
helped me with same error
In .eslintrc
, specify the type:
{
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
}
}