Javascript 打字脚本+;lodash-找不到模块
我试图让typescript和lodash在我的应用程序中工作,但当我运行 tsc——模块commonjs lodash-test.ts 我总是很紧张 lodash test.ts(2,20):错误TS2307:找不到模块“lodash” 这仍然会生成一个lodash-test.js文件Javascript 打字脚本+;lodash-找不到模块,javascript,typescript,lodash,Javascript,Typescript,Lodash,我试图让typescript和lodash在我的应用程序中工作,但当我运行 tsc——模块commonjs lodash-test.ts 我总是很紧张 lodash test.ts(2,20):错误TS2307:找不到模块“lodash” 这仍然会生成一个lodash-test.js文件 "use strict"; //lodash test that will be injected in index.html var _ = require("lodash"); console.log('T
"use strict";
//lodash test that will be injected in index.html
var _ = require("lodash");
console.log('This is your lodash ts file');
console.info(_.chunk(['a', 'b', 'c', 'd'], 2));
现在,当我在浏览器中运行index.html文件时
lodash test.js:3未捕获引用错误:未定义require
在文件的第3行
Index.html文件
嘿
我的打字文件夹中有lodash类型定义。如何确保Typescript可以找到这个lodash/index.d.ts文件?另外,如何修复“未定义需求”错误
这是我的文件结构:
感谢您的帮助代码>对于加载脚本的方式不是必需的
在您的
中有一行:
<script type="text/javascript" src="bower_components/lodash/dist/lodash.min.js"></script>
它将加载到全局命名空间中require(“lodash”)
是一个在运行时加载lodash的模块加载器
因此:
A) 删除require
语句,只需使用var
,就好像它已经被加载一样(因为它已经被加载了)。您可能需要使用declare
关键字来做一些棘手的事情,以使tsc
不会抱怨\uu
未定义
B) 在您的
中删除lodash的
,并正确设置require
,以便它知道什么是“lodash”
。否则,可能能够简单地执行var=require('bower\u components/lodash/dist/lodash.min')代码>var=require(“lodash”)代码>对于加载脚本的方式不是必需的
在您的
中有一行:
<script type="text/javascript" src="bower_components/lodash/dist/lodash.min.js"></script>
它将加载到全局命名空间中require(“lodash”)
是一个在运行时加载lodash的模块加载器
因此:
A) 删除require
语句,只需使用var
,就好像它已经被加载一样(因为它已经被加载了)。您可能需要使用declare
关键字来做一些棘手的事情,以使tsc
不会抱怨\uu
未定义
B) 在您的
中删除lodash的
,并正确设置require
,以便它知道什么是“lodash”
。否则,可能能够简单地执行var=require('bower\u components/lodash/dist/lodash.min')
尝试导入d.ts
打字文件,并在TypeScript文件顶部使用/
命令。因为现在,tsc
已经提示您有打字文件。所以它找不到了。@Blackus我把我的lodash-test.ts改成了,它产生了图像右侧的内容。我还删除了index.html中的lodash库。我仍然收到相同的错误尝试导入d.ts
typings文件,并在TypeScript文件顶部使用//
命令。因为现在,tsc
已经提示您有打字文件。所以它找不到了。@Blackus我把我的lodash-test.ts改成了,它产生了图像右侧的内容。我还删除了index.html中的lodash库。我仍然得到同样的错误