【问题标题】:How to translate to typescript : require('module').globalPaths.push(process.env.NODE_MODULES_PATH);如何翻译成打字稿: require('module').globalPaths.push(process.env.NODE_MODULES_PATH);
【发布时间】:2019-02-11 21:00:48
【问题描述】:

我使用 vue ui 用 typescript 创建了一个 vue 项目,然后我添加了 electron、iview 和 less ...

我只纠正了 *.ts 文件中的词法问题,但是当我在 vue ui 中运行 vue-cli-service 服务任务时,我得到以下输出:

Total task duration: 1191.84s
$ vue-cli-service serve --mode development --dashboard
 INFO  Starting development server...

Starting type checking and linting service...

 12% building modules 23/53 modules 30 active ...ode_modules/core-js/modules/_perform.jsUsing 1 worker with 2048MB memory limit

 85% chunk order optimization OccurrenceOrderChunkIdsPlugin ERROR  Failed to compile with 1 errors11:31:30 PM


 error  in ./src/iview-variables.less

Module build failed (from ./node_modules/less-loader/dist/cjs.js):


// https://github.com/ant-design/ant-motion/issues/44
.bezierEasingMixin();
^
Inline JavaScript is not enabled. Is it set in your options?
  in /Users/ailete619/landema_electron/node_modules/iview/src/styles/color/bezierEasing.less (line 110, column 0)

 @ ./src/iview-variables.less 4:14-226 14:3-18:5 15:22-234
 @ ./src/plugins/iview.js
 @ ./src/main.ts
 @ multi (webpack)-dev-server/client?http://192.168.179.4:8080/sockjs-node (webpack)/hot/dev-server.js ./src/main.ts

Type checking and linting in progress...

 WARN
Couldn't parse bundle asset  "/Users/ailete619/landema_electron/dist/about.js".
Analyzer will use module sizes from stats file.


WARNING in /Users/ailete619/landema_electron/src/background.ts
13:3 require statement not part of an import statement
    11 | if (isDevelopment) {
    12 |   // Don't load any native (external) modules until the following line is run:
  > 13 |   require('module').globalPaths.push(process.env.NODE_MODULES_PATH);
       |   ^
    14 | }
    15 | // global reference to mainWindow (necessary to prevent window from being garbage collected)
    16 | let mainWindow: any;    
No type errors found
Version: typescript 3.0.3, tslint 5.11.0
Time: 3084ms

我尝试修改以下几行,但我并不真正理解它们的目的,所以我得到了不同的错误但没有解决方案:

if (isDevelopment) {
  // Don't load any native (external) modules until the following line is run:
  require('module').globalPaths.push(process.env.NODE_MODULES_PATH)
}

我的问题:如何更改该行以使用 typescript?

PS:对于 Module build failed 错误,我正在处理它,如果有必要会问另一个问题...

已安装的插件:

  • @vue/cli-service 3.01版
  • @vue/cli-plugin-babel 3.0.1 版
  • @vue/cli-plugin-pwa 3.0.1 版
  • @vue/cli-plugin-typescript 3.0.1 版
  • vue-cli-plugin-electron-builder 版本 1.0.0-rc2
  • vue-cli-plugin-iview 版本 1.0.6

主要依赖:

  • iview 3.1.0 版
  • register-service-worker 版本 1.5.2
  • vue 版本 2.5.17
  • vue-class-component version6.2.0
  • vue-property-decorator 版本 7.0.0
  • vue-router 3.0.1版
  • vuex 版本 3.0.1
  • 电子版 2.0.8
  • 低于 3.8.1 版
  • less-loader 版本 4.0.1
  • 打字稿版本 3.0.3
  • vue-template-compiler 版本 2.5.17

建议的解决方案 1: 将语句分成 2 会引发新的错误 ...

ERROR in /Users/ailete619/landema_electron/src/background.ts
11:1 Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.
     9 | } from 'vue-cli-plugin-electron-builder/lib';
    10 | const isDevelopment = process.env.NODE_ENV !== 'production';
  > 11 | import module = require('module');
       | ^
    12 | if (isDevelopment) {
    13 |   // Don't load any native (external) modules until the following line is run:
    14 |   module.globalPaths.push(process.env.NODE_MODULES_PATH);
ERROR in /Users/ailete619/landema_electron/src/background.ts
14:10 Property 'globalPaths' does not exist on type 'typeof Module'.
    12 | if (isDevelopment) {
    13 |   // Don't load any native (external) modules until the following line is run:
  > 14 |   module.globalPaths.push(process.env.NODE_MODULES_PATH);
       |          ^
    15 | }
    16 | // global reference to mainWindow (necessary to prevent window from being garbage collected)
    17 | let mainWindow: any;

我的解决方案:我删除了它,只剩下不太相关的错误...问题解决了吗?

【问题讨论】:

    标签: typescript vuejs2 less electron iview


    【解决方案1】:

    试试这个:

    import module = require('module');
    if (isDevelopment) {
      // Don't load any native (external) modules until the following line is run:
      module.globalPaths.push(process.env.NODE_MODULES_PATH)
    }
    

    【讨论】:

    • 感谢您的快速回答,但我遇到了新错误。
    猜你喜欢
    • 1970-01-01
    • 2017-11-07
    • 2019-03-01
    • 2017-12-20
    • 2021-08-21
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多