Cannot find "shelljs" module to enable lib in angular 2 typescript

I want to include the shelljs library in angular 2 typescript. I have included the shelljs.d.ts file in my node_modules / shelljs library.

My package.json

"name": "myproj1", "description": "myproj1: A project", "typings": { "shelljs": { "definitions": "node_modules/shelljs/shelljs.d.ts", "source": "node_modules/shelljs/global.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' }; 

My package.json compiler options:

 "compilerOptions": { "target": "es5", "module": "commonjs", "moduleResolution": "node", "emitDecoratorMetadata": true, "experimentalDecorators": true }, 

My TS file:

 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); }); } 

I get the error β€œI can’t find the shelljs module. Please help me include the library in my project.

+1
source share
1 answer

Use tsd to manage all your typings.

From the project directory:

 npm install tsd -g tsd install node --save tsd install shelljs --save 

Then include the shelljs link in foo.ts :

 /// <reference path="typings/shelljs/shelljs.d.ts" /> import {exec} from "shelljs"; exec('node --version', code => { console.log('Exit code:', code); }); 

Based on the comments, here is a summary:

shelljs can only be used in NodeJS environments. It can be either a raw NodeJS instance, or some projects that contain nodejs in self, for example Electron .

You should also pay attention to which modular system you use. For NodeJS you can use CommonJS without any additional packages. But if you compile your TypeScripts for an interface where NodeJS not, then you should also combine your CommonJS modules with browserify . Or you can use other types of modules, for example amd or SystemJS , and then in the typescript compiler options you set `` module ': "system", for example. See all options here.

0
source

All Articles