我想在chrome扩展和node中重用部分代码。导入不适用于节点。 module.export在扩展中不起作用

时间:2019-05-25 07:59:14

标签: javascript node.js google-chrome-extension

我正在尝试使用chrome扩展名在chrome之间创建套接字连接。我正在节点js中的Google App引擎上使用Web套接字。

代码的某些部分在两种环境中都是通用的,我正在尝试重用它。

exportimport语句在节点

中不起作用

module.exportrequire语句在chrome extesnion中不起作用

对于节点

// app.js node
import { getRandomPushKey } from '../script/Utils/utils';
//utils.js
module.exports =  { getRandomPushKey };

错误

$ node --experimental-modules app.js
(node:18980) ExperimentalWarning: The ESM module loader is experimental.
/Users/chandrakumar/Desktop/projects/test on the fly/server/app.js:2
import { getRandomPushKey } from '../script/Utils/utils';
       ^

SyntaxError: Unexpected token {
    at Module._compile (internal/modules/cjs/loader.js:703:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
    at Module.load (internal/modules/cjs/loader.js:628:32)
    at Function.Module._load (internal/modules/cjs/loader.js:555:12)
    at internal/modules/esm/translators.js:84:15
    at Object.meta.done (internal/modules/esm/create_dynamic_module.js:39:9)
    at file:///Users/chandrakumar/Desktop/projects/test%20on%20the%20fly/server/app.js:9:13
    at ModuleJob.run (internal/modules/esm/module_job.js:111:37)
    at processTicksAndRejections (internal/process/task_queues.js:89:5)
    at async Loader.import (internal/modules/esm/loader.js:128:24)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

如果我给了

//node
const { getRandomPushKey } = require('../script/Utils/utils');
//js
export default  { getRandomPushKey };

错误

yarn run v1.16.0
$ node --experimental-modules app.js
(node:19021) ExperimentalWarning: The ESM module loader is experimental.
/Users/chandrakumar/Desktop/projects/test on the fly/script/Utils/utils.js:10
export default  { getRandomPushKey };
^^^^^^

SyntaxError: Unexpected token export
    at Module._compile (internal/modules/cjs/loader.js:703:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
    at Module.load (internal/modules/cjs/loader.js:628:32)
    at Function.Module._load (internal/modules/cjs/loader.js:555:12)
    at Module.require (internal/modules/cjs/loader.js:666:19)
    at require (internal/modules/cjs/helpers.js:16:16)
    at Object.<anonymous> (/Users/chandrakumar/Desktop/projects/test on the fly/server/app.js:2:30)
    at Module._compile (internal/modules/cjs/loader.js:759:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
    at Module.load (internal/modules/cjs/loader.js:628:32)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

如果是Chrome扩展名

module.exports = { getRandomPushKey };和chrome扩展名js import { getRandomPushKey } from "../Utils/utils.js";中,此错误在chrome扩展名中抛出background.html:1 Uncaught SyntaxError: The requested module '../Utils/utils.js' does not provide an export named 'getRandomPushKey'

如果我提供 const { getRandomPushKey } = require("../Utils/utils.js"); 在chrome ext js中,此错误在chrome中抛出 StorageHelpers.js:3 Uncaught ReferenceError: require is not define at StorageHelpers.js:3

节点版本:v12.1.0

Chrome版本:74.0.3729.169

Package.json

....
"engines": {
    "node": ">=10.15.0"
  },
"types": "module",
"scripts": {
   ...,
   "start": "node --experimental-modules app.js",
 }
....

manifest.json

{
  "name": "record and replay",
  "manifest_version": 2,
  "version": "1.0.0",
  "permissions": [ "declarativeContent", "storage", "activeTab", "tabs", 
    "http://localhost:8080/", "identity" ],
  "browser_action": {
    "default_popup": "popup.html"
  },
  "background": {
    "page": "background.html",
    "persistent": false
  },
  "web_accessible_resources": [
    "script/*"
  ]
}

0 个答案:

没有答案