简体   繁体   English

NPM + Browserify 错误:ParseError: 'import' 和 'export' 可能只出现在 'sourceType: module' 中

[英]NPM + Browserify error: ParseError: 'import' and 'export' may appear only with 'sourceType: module'

I have two simple js files.我有两个简单的 js 文件。 One of them should be the module which exports its content which will be included by import command.其中之一应该是导出其内容的模块,该模块将包含在 import 命令中。

First testModule.js:首先 testModule.js:

export function test() {
    console.log('teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeest');
}

and main.js:和 main.js:

import * as test from "../src/js/testModule.js"

test.test();

In package.json I have two script commands.在 package.json 我有两个脚本命令。 First to translate es6 to es5 and second to Browserify the es5 result because of modules.首先将 es6 翻译为 es5,然后将 es5 结果浏览器化,因为模块。 First one works.第一个有效。 But the second throws me an error.但是第二个给我一个错误。

{
    "name": "examples-vue",
    "main": "index.js",
    "dependencies": {
        "bootstrap": "^3.4.1",
        "jquery": "^3.4.1",
        "vue": "^2.6.11"
    },
    "devDependencies": {
        "@babel/cli": "^7.10.4",
        "@babel/core": "^7.10.4",
        "@babel/polyfill": "^7.10.4",
        "@babel/preset-env": "^7.10.4",
        "browserify": "^16.5.1",
        "core-js": "^3.6.5"
    },
    "scripts": {
        "es5": "babel ./src/js/main.js -o ./js/main.es5.js",
        "browserify": "browserify ./js/main.es5.js -o ./js/bundle.js",
        "build": "npm run es5 && npm run browserify"
    }
}

The result of the first command is this:第一个命令的结果是这样的:

"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

var test = _interopRequireWildcard(require("../src/js/testModule.js"));

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

test.test();

And the second command throws an error: ParseError: 'import' and 'export' may appear only with 'sourceType: module'第二个命令抛出错误: ParseError: 'import' and 'export' may appear only with 'sourceType: module'

In other questions is still the same answer.在其他问题中仍然是相同的答案。 Browserify does not work with es6 syntax. Browserify 不适用于 es6 语法。 But It works with es5 file.但它适用于 es5 文件。 What is wrong with this code?这段代码有什么问题? Thanks a lot for any help.非常感谢您的帮助。

So the valid syntax for Browserify is所以 Browserify 的有效语法是

for import用于进口

import * as testModule from "./testModule.js";
import { test2module } from "./testModule2.js";

for export用于出口

module.exports.test = test;
module.exports.xxx = xxx;

or for export或出口

export var test2module = {
    test2: test2,
    aaa: aaa
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ParseError: 'import' 和 'export' 可能只出现在 'sourceType: module', browserify - ParseError: 'import' and 'export' may appear only with 'sourceType: module', browserify Browserify Rails错误-ParseError:“导入”和“导出”可能仅与“ sourceType:模块”一起出现 - Browserify Rails Error - ParseError: 'import' and 'export' may appear only with 'sourceType: module' cypress + lerna: ParseError: 'import' 和 'export' 可能只出现在 'sourceType: module' 中 - cypress + lerna: ParseError: 'import' and 'export' may appear only with 'sourceType: module' Cypress ParseError: 'import' 和 'export' 可能只出现在 'sourceType: module' - Cypress ParseError: 'import' and 'export' may appear only with 'sourceType: module' 'import' 和 'export' 只能与 'sourceType: "module"' 一起出现 (16:0) - 'import' and 'export' may appear only with 'sourceType: "module"' (16:0) SyntaxError: 'import' 和 'export' 可能只与 'sourceType: module' 一起出现 - Gulp - SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' - Gulp create-react-app eslint 错误“解析错误:‘import’和‘export’可能只出现在‘sourceType:module’中” - create-react-app eslint error "Parsing error: 'import' and 'export' may appear only with 'sourceType: module'" 开玩笑的返回错误:SyntaxError:“导入”和“导出”可能仅与“ sourceType:“模块””一起出现(21:0) - jest return ERROR: SyntaxError: 'import' and 'export' may appear only with 'sourceType: “module”' (21:0) Babel 7 不转译 node_modules 中的依赖项:'import' 和 'export' 可能只与 'sourceType: module' 一起出现 - Babel 7 not transpiling dependencies in node_modules: 'import' and 'export' may appear only with 'sourceType: module' 如何告诉 eslint:禁用下一行“'import'和'export'可能只与'sourceType:module'一起出现” - How to tell eslint to: disable next line "'import' and 'export' may appear only with 'sourceType: module'"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM