DEV Community

ChuangWANG
ChuangWANG

Posted on

Create a npm project and install typescript dependency

Type of package installation with npm:
npm install -g packageName Install global
npm install --save -dev pacakgeName Install locally
npm install --save packageName Install locally
when run npm install --production or env variable NODE_ENV value is production, only packages in dependencies will be install, but packages in devDependencies will not.

npm install -g packageName
npm install --save-dev packageName
npm install --save packageName
Enter fullscreen mode Exit fullscreen mode

Create a new folder for this project
run npm init in the work folder above mentioned

npm init
Enter fullscreen mode Exit fullscreen mode

after we run npm init, we will get a new file named package.json,the file content as bellow:

{
  "name": "typescript-learn",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}
Enter fullscreen mode Exit fullscreen mode

now we should insert code line "type": "module", in package.json to support ES5

After we insert the new code line ,the package.json as bellow:

{
  "name": "typescript-learn",
  "version": "1.0.0",
  "description": "",
  "type": "module",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Enter fullscreen mode Exit fullscreen mode

note: let the browser support ES5 ,we can insert line as bellow:

<script type="module" src="xxxxx.js"></script>
Enter fullscreen mode Exit fullscreen mode

Now, we will install typescript dependency:
npm install typescript --save-dev

This demo location: code-repo/learn-test/typescript-learn
Git link:

Top comments (0)