te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Getting the "Module parse failed: 'import' and 'export' may appear only wi
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting the "Module parse failed: 'import' and 'export' may appear only wi

programmeradmin3浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 11

If 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
        }
    }
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论