2016-06-25 22 views
14

Voglio installare ed eseguire Typescript (cioè nessuna dipendenza globale).Come installare ed eseguire Typescript localmente in npm?

Ecco il mio file package.json:

{ 
    "name": "foo", 
    "version": "1.0.0", 
    "description": "", 
    "main": "index.js", 
    "scripts": { 
    "test": "echo \"Error: no test specified\" && exit 1", 
    "tsc": "tsc" 
    }, 
    "devDependencies": { 
    "typescript": "^1.8.10" 
    }, 
    "author": "", 
    "license": "ISC" 
} 

ho quindi eseguire:

npm install 
npm run tsc 

Tuttavia quando ho eseguito il secondo comando ottengo sooo molti errori non è possibile visualizzare tutto ciò. La maggior parte è simile al seguente:

../foo/node_modules/typescript/lib/lib.d.ts(5015,5): error TS2300: Duplicate identifier 'webkitTransformOrigin'. 
../foo/node_modules/typescript/lib/lib.d.ts(5016,5): error TS2300: Duplicate identifier 'webkitTransformStyle'. 
../foo/node_modules/typescript/lib/lib.d.ts(5017,5): error TS2300: Duplicate identifier 'webkitTransition'. 
../foo/node_modules/typescript/lib/lib.d.ts(5018,5): error TS2300: Duplicate identifier 'webkitTransitionDelay'. 
../foo/node_modules/typescript/lib/lib.d.ts(5019,5): error TS2300: Duplicate identifier 'webkitTransitionDuration'. 
../foo/node_modules/typescript/lib/lib.d.ts(5020,5): error TS2300: Duplicate identifier 'webkitTransitionProperty'. 

In NPM-debug.log ottengo:

0 info it worked if it ends with ok 
1 verbose cli [ '/usr/bin/nodejs', '/usr/bin/npm', 'run', 'tsc' ] 
2 info using [email protected] 
3 info using [email protected] 
4 verbose run-script [ 'pretsc', 'tsc', 'posttsc' ] 
5 info lifecycle [email protected]~pretsc: [email protected] 
6 silly lifecycle [email protected]~pretsc: no script for pretsc, continuing 
7 info lifecycle [email protected]~tsc: [email protected] 
8 verbose lifecycle [email protected]~tsc: unsafe-perm in lifecycle true 
9 verbose lifecycle [email protected]~tsc: PATH: /usr/lib/node_modules/npm/bin/node-gyp-bin:/home/vagrant/foo/node_modules/.bin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games 
10 verbose lifecycle [email protected]~tsc: CWD: /home/vagrant/foo 
11 silly lifecycle [email protected]~tsc: Args: [ '-c', 'tsc' ] 
12 silly lifecycle [email protected]~tsc: Returned: code: 2 signal: null 
13 info lifecycle [email protected]~tsc: Failed to exec tsc script 
14 verbose stack Error: [email protected] tsc: `tsc` 
14 verbose stack Exit status 2 
14 verbose stack  at EventEmitter.<anonymous> (/usr/lib/node_modules/npm/lib/utils/lifecycle.js:242:16) 
14 verbose stack  at emitTwo (events.js:100:13) 
14 verbose stack  at EventEmitter.emit (events.js:185:7) 
14 verbose stack  at ChildProcess.<anonymous> (/usr/lib/node_modules/npm/lib/utils/spawn.js:40:14) 
14 verbose stack  at emitTwo (events.js:100:13) 
14 verbose stack  at ChildProcess.emit (events.js:185:7) 
14 verbose stack  at maybeClose (internal/child_process.js:850:16) 
14 verbose stack  at Process.ChildProcess._handle.onexit (internal/child_process.js:215:5) 
15 verbose pkgid [email protected] 
16 verbose cwd /home/vagrant/foo 
17 error Linux 3.13.0-88-generic 
18 error argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "tsc" 
19 error node v5.12.0 
20 error npm v3.10.2 
21 error code ELIFECYCLE 
22 error [email protected] tsc: `tsc` 
22 error Exit status 2 
23 error Failed at the [email protected] tsc script 'tsc'. 
23 error Make sure you have the latest version of node.js and npm installed. 
23 error If you do, this is most likely a problem with the foo package, 
23 error not with npm itself. 
23 error Tell the author that this fails on your system: 
23 error  tsc 
23 error You can get information on how to open an issue for this project with: 
23 error  npm bugs foo 
23 error Or if that isn't available, you can get their info via: 
23 error  npm owner ls foo 
23 error There is likely additional logging output above. 
24 verbose exit [ 1, true ] 

Nota che la rimozione del pacchetto e quindi l'installazione di dattiloscritto a livello globale risolve il problema. Tuttavia se poi uso npm install per installare nuovamente i pacchetti locali, reintroduce il problema.

risposta

10

Per installare dattiloscritto locale in progetto come uno sviluppo della dipendenza è possibile utilizzare --save-dev chiave

npm install --save-dev typescript 

Si scrive anche il dattiloscritto nel vostro package.json

È inoltre necessario disporre di un file tsconfig.json. Per esempio

{ 
    "compilerOptions": { 
    "target": "ES5", 
    "module": "system", 
    "moduleResolution": "node", 
    "sourceMap": true, 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "removeComments": false, 
    "noImplicitAny": false 
    }, 
    "exclude": [ 
    "node_modules", 
    ".npm" 
    ] 
} 

Per ulteriori informazioni sulla TSconfig potete vedere qui http://www.typescriptlang.org/docs/handbook/tsconfig-json.html

+0

come è diverso da quello che ho fatto nel mio 'package.json'. Tutto ciò che fa è installare script di tipo e salvarlo nelle mie dipendenze. È anche una dipendenza dev (non una dipendenza normale) quindi dovrebbe essere 'npm install --dev-save' –

+0

@YahyaUddin hai un' tsconfig.json'? – Mikhail

+0

no. Aggiungendo questo ora –

2

tsc richiede un file di configurazione o .ts (x) i file per la compilazione.

Per risolvere entrambe le problematiche, creare un file chiamato tsconfig.json con il seguente contenuto:

{ 
    "compilerOptions": { 
     "outFile": "../../built/local/tsc.js" 
    }, 
    "exclude": [ 
     "node_modules" 
    ] 
} 

Inoltre, modificare la corsa NPM con questo

tsc --config /path/to/a/tsconfig.json 
+0

Ho solo bisogno della parte di esclusione affinché funzioni. Inoltre, non è necessario utilizzare il parametro '- config' perché il dattiloscritto guarda lì automaticamente fornito alla radice del progetto. –

+0

Grazie. Non ero a conoscenza di quello – Bikas

0

Nota se si utilizza typings fare il seguente:

rm -r typings 
typings install 

Se stai facendo e angolare 2 esercitazione usate questo:

rm -r typings 
npm run postinstall 
npm start 

se il lavoro dosen't postinstall comando, provare a installare tipaggi globalmente modo:

npm install -g typings 

è anche possibile provare il seguente anziché postinstall:

typings install 

e si dovrebbe risolvere questo problema!

12

Mi ci è voluto un po 'per capire la soluzione a questo problema - è nella domanda originale.È necessario disporre di un script che chiama tsc nel file package.json in modo che sia possibile eseguire:

npm run tsc 

Includere -- prima di passare a opzioni (o semplicemente includere nello script):

npm run tsc -- -v 

Ecco un esempio package.json:

{ 
    "name": "foo", 
    "scripts": { 
    "tsc": "tsc" 
    }, 
    "dependencies": { 
    "typescript": "^1.8.10" 
    } 
} 
+1

Problema difficile a google! Funziona con pacchetti cli che suggeriscono di installarlo a livello globale, ma li vogliono solo localmente, grazie! – Cammy

2

dovete dire NPM che "TSC" esiste come un pacco progetto locale età (tramite la proprietà "scripts" nel pacchetto package.json) e quindi eseguirlo tramite npm run tsc. Per fare questo (almeno su Mac) ho dovuto aggiungere il percorso per il compilatore reale all'interno del pacchetto, come questo

{ 
    "name": "foo" 
    "scripts": { 
    "tsc": "./node_modules/typescript/bin/tsc" 
    }, 
    "dependencies": { 
    "typescript": "^2.3.3", 
    "typings": "^2.1.1" 
    } 
} 

Dopo di che è possibile eseguire qualsiasi comando dattiloscritto come npm run tsc -- --init (gli argomenti vengono dopo il primo --).