从Webpack 2中的js文件导入模块
我尝试在Webpack 2中使用以下语法:从Webpack 2中的js文件导入模块,webpack,require,webpack-2,Webpack,Require,Webpack 2,我尝试在Webpack 2中使用以下语法: import someSvc = require("./some.svc.js"); 但我得到了一个错误: error TS2307: Cannot find module './some.svc.js'. 我做错了什么?!如何在Webpack 2中导入我的js模块 为了彻底起见,我将我的项目归结为最微小的示例,并将提供以下文件: webpack.config.js package.json tsconfig.js src/main.ts src/
import someSvc = require("./some.svc.js");
但我得到了一个错误:
error TS2307: Cannot find module './some.svc.js'.
我做错了什么?!如何在Webpack 2中导入我的js模块
为了彻底起见,我将我的项目归结为最微小的示例,并将提供以下文件:
webpack.config.js
package.json
tsconfig.js
src/main.ts
src/some.svc.js
将这些文件粘在一起,运行webpack
,您应该会看到相同的错误
我是不是错过了一些简单的方法来让它发挥作用?在与之斗争太久之后,像往常一样,写SO帖子不知怎么地触发了我的大脑或其他东西 从SystemJs到Webpack,我根本没想到
require
会改变
不过,我做了两个修改来解决这个问题
这一行:
导入someSvc=require(“./some.svc.js”)代码>
变成:
var-someSvc=require(“./some.svc.js”)代码>
运行这个(我使用的是TypeScript 2.0):
npm安装@types/node——保存开发
var path = require('path');
module.exports = function makeWebpackConfig() {
var config = {};
config.entry = { 'app': './src/main.ts' };
config.output = {
path: root('dist'),
filename: 'js/[name].js'
};
config.module = {
rules: [
{
test: /\.ts$/,
loaders: ['ts-loader']
}
]
};
return config;
}();
// Helper functions
function root(args) {
args = Array.prototype.slice.call(arguments, 0);
return path.join.apply(path, [__dirname].concat(args));
}
{
"name": "webpack",
"version": "1.0.0",
"description": "",
"main": "src/main.ts",
"dependencies": {},
"devDependencies": {
"ts-loader": "^0.9.5",
"typescript": "^2.0.3"
},
"scripts": {},
"author": "",
"license": "ISC"
}
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"noEmitHelpers": true
},
"compileOnSave": false,
"buildOnSave": false,
"exclude": [
"node_modules"
]
}
import someSvc = require("./some.svc.js");
if (typeof module !== 'undefined' && module.exports) {
module.exports.config = function (conf) {
return { abc: 123 };
};
}