Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在节点的TypeScript中使用非类型化JS NPM模块?_Javascript_Node.js_Typescript_Npm - Fatal编程技术网

Javascript 如何在节点的TypeScript中使用非类型化JS NPM模块?

Javascript 如何在节点的TypeScript中使用非类型化JS NPM模块?,javascript,node.js,typescript,npm,Javascript,Node.js,Typescript,Npm,我刚开始尝试TypeScript,我有以下项目结构: -src | index.ts | MyClass.ts -node_modules -npm_js_module | index.js | utils.js - src | someModuleFile.js | someModuleFile2.js 我需要在MyClass.ts中使用npm_js_module/index.js(和pm_js_module/src)中的函数,但index.js需要这些函数。此模块未键入,其

我刚开始尝试TypeScript,我有以下项目结构:

-src
| index.ts
| MyClass.ts
-node_modules
 -npm_js_module
 | index.js
 | utils.js
 - src
  | someModuleFile.js
  | someModuleFile2.js
我需要在MyClass.ts中使用npm_js_module/index.js(和pm_js_module/src)中的函数,但index.js需要这些函数。此模块未键入,其他人无法在线键入。甚至可以在typescript项目中使用非类型化JavaScript NPM模块吗

当我试图编译此项目时,我遇到以下错误:

error TS6059: File '/node_modules/npm_js_module/index.js' is not under 'rootDir' '/src'. 'rootDir' is expected to contain all source files.
error TS6059: File '/node_modules/npm_js_module/utils.js'' is not under 'rootDir' '/src'. 'rootDir' is expected to contain all source files.
我的tsconfig.json如下所示:

{
  "compilerOptions": {
    "moduleResolution": "node",
    "module": "commonjs",
    "target": "es2017",
    "outDir": "./bin",
    "rootDir": "./src",
    "sourceMap": true,
    "allowJs" : true,
    "baseUrl" : "./",
    "paths": {
      "*" : ["./node_modules/@types/*", "*"]
    }
  },
  "files": [
    "./node_modules/@types/node/index.d.ts",
    "./node_modules/npm_js_module/index.js"
  ],
  "include": [
    "src/**/*.ts",
    "./node_modules/npm_js_module/*"
  ],
  "exclude": [
    "./node_modules"
  ]
}
{
  "name": "myproject",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/node": "^8.0.30"
  },
  "dependencies": {
    "npm_js_module": "^1.2.3"
  }
}
和package.json,如下所示:

{
  "compilerOptions": {
    "moduleResolution": "node",
    "module": "commonjs",
    "target": "es2017",
    "outDir": "./bin",
    "rootDir": "./src",
    "sourceMap": true,
    "allowJs" : true,
    "baseUrl" : "./",
    "paths": {
      "*" : ["./node_modules/@types/*", "*"]
    }
  },
  "files": [
    "./node_modules/@types/node/index.d.ts",
    "./node_modules/npm_js_module/index.js"
  ],
  "include": [
    "src/**/*.ts",
    "./node_modules/npm_js_module/*"
  ],
  "exclude": [
    "./node_modules"
  ]
}
{
  "name": "myproject",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/node": "^8.0.30"
  },
  "dependencies": {
    "npm_js_module": "^1.2.3"
  }
}

我使用这种方式导入该模块:
import*作为“npm_js_module”中的mymodule
其中VScode在代码中显示此错误:
属性“login”不存在于type”(loginda:any,options:any,callback:any)=>void.
当我尝试编译此代码时,会出现类似于我上面所写的错误。

它不是非常清晰,但它隐藏在文档中:

如果向下滚动,您将看到一个标有: 导出=和导入=需要()

使用export=导入模块时,必须使用TypeScript特定的import module=require(“模块”)来导入模块

这是一个例子:

import zip = require("./ZipCodeValidator");

希望这能有所帮助。

好吧,有3种可能的解决方案

  • 使用普通Javascript
    import jsModule=require('npm_js_module')
  • 为您的模块创建一个虚拟定义并手动包含它
  • 最佳方法:创建正确的定义并向官方存储库发出请求

  • 第一个解决方案应该适合您,只需要将其作为普通的node.js模块。(如果您使用的是最新版本的node 8.5,您将能够通过激活特殊标志
    节点--experimental modules main.mjs
    )来使用ES6导入样式,但我不推荐使用它,因为它还没有最终确定。

    我找到了该模块的输入,因此不需要进行任何黑客攻击,谢谢大家。我尝试了@Will的解决方案,但效果太好,太晚了。