我想将 shelljs 库包含到 Angular 2 TypeScript 中。我已将 shelljs.d.ts 文件包含到我的 node_modules/shelljs 库中。
我的package.json
"name": "myproj1",
"description": "myproj1: A project",
"typings": {
"shelljs": {
"definitions": "node_modules/shelljs/shelljs.d.ts",
"source": "node_modules/shelljs/global.js"
}
},
我的 webpack.config.js
var path = require('path');
module.exports = {
entry: './app/web/boot.ts',
output: {
path: path.resolve(__dirname, "js"),
filename: "bundle.js"
},
resolve: {
extensions:['','.js','.ts']
},
module:{
loaders: [{
test: /\.ts/,
loaders: ['ts-loader'],
exclude: /node_modules/
}]
},
target: 'node'
};
我的 package.json 编译器选项:
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
我的 TS 文件:
import child = require("shelljs");
somefun(){
child.exec('node --version',(code, stdout, stderr)=>{
console.log('Exit code:', code);
console.log('Program output:', stdout);
console.log('Program stderr:', stderr);
});
}
我收到错误“找不到模块‘shelljs’”。请帮助我将该库包含在我的项目中。
最佳答案
使用tsd
管理您的所有打字。
来自您的项目目录:
npm install tsd -g
tsd install node --save
tsd install shelljs --save
然后在您的 foo.ts
中包含对 shelljs 的引用:
/// <reference path="typings/shelljs/shelljs.d.ts" />
import {exec} from "shelljs";
exec('node --version', code => {
console.log('Exit code:', code);
});
根据评论,总结如下:
只能在 NodeJS
环境中使用 shelljs
。这可以是原始的 nodejs
实例,也可以是一些自身包含 Nodejs 的项目,例如 Electron
。
您还应该注意您正在使用哪个模块系统。对于 NodeJS
,您可以使用 CommonJS
,无需任何其他捆绑程序。但是,如果您为前端编译 TypeScript,其中不存在 NodeJS
,那么您还应该将 CommonJS
模块与 browserify
捆绑在一起。 。或者您可以使用其他类型的模块,例如 amd
或SystemJS
,然后在 typescript 编译器选项中设置 `"module": "system"例如。查看所有选项 here
关于node.js - 在 Angular 2 typescript 中包含 lib 时找不到模块 "shelljs",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36913677/