找不到模块“ shelljs”;关于在lib 2打字稿中包含lib [英] Cannot find module "shelljs" on including the lib in angular 2 typescript
问题描述
我想将shelljs库包含到angular 2打字稿中。我已经将shelljs.d.ts文件包含到我的node_modules / shelljs库中。
I want to include shelljs library to angular 2 typescript. I have included the shelljs.d.ts file to my node_modules/shelljs library.
我的package.json
My 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
My 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编译器选项:
My package.json compiler options:
"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'。请帮助我将库包含在我的项目中。
I am getting the error as "Cannot find module 'shelljs'". Please help me to include the library in my project.
推荐答案
使用 tsd
来管理所有类型。
Use tsd
to manage all your typings.
从您的项目目录中:
npm install tsd -g
tsd install node --save
tsd install shelljs --save
然后在<$ c $中包含对shelljs的引用c> foo.ts :
/// <reference path="typings/shelljs/shelljs.d.ts" />
import {exec} from "shelljs";
exec('node --version', code => {
console.log('Exit code:', code);
});
根据评论,以下是摘要:
仅在 NodeJS
环境中可以使用 shelljs
。这可以是原始的 nodejs
实例,也可以是一些自身包含nodejs的项目,例如 电子
。
Using shelljs
is only possible in NodeJS
environment. This can be either raw nodejs
instance, or some projects which contain nodejs in self, like Electron
.
还应注意所使用的模块系统。对于 NodeJS
,您可以使用 CommonJS
,而无需任何其他捆绑程序。但是,如果您为不存在 NodeJS
的前端编译TypeScript,那么您还应该捆绑您的 CommonJS
模块使用 browserify
。或者,您可以使用其他类型的模块,例如 amd
或< a href = https://github.com/systemjs/systemjs rel = nofollow> SystemJS
,然后在Typescript编译器选项中进行设置` module:例如 system。在此处
Also you should pay attention, which Module System you are using. For NodeJS
you can use CommonJS
without any further bundlers. But if you compile your TypeScripts for the frontend, where NodeJS
is not present, then you should also bundle your CommonJS
modules with browserify
. Or you can use other types of Modules, like amd
or SystemJS
, and then in typescript compiler options, you set `"module": "system" for instance. See all options here
这篇关于找不到模块“ shelljs”;关于在lib 2打字稿中包含lib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!