3分钟搞定NPM模块开发

3分钟搞定NPM模块开发

创建组建

新建目录

localhost:Desktop yuechunli$ pwd
/Users/liyuechun/Desktop
localhost:Desktop yuechunli$ mkdir liyc_hello
localhost:Desktop yuechunli$ cd liyc_hello/

初始化

localhost:liyc_hello yuechunli$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (liyc_hello) 
version: (1.0.0) 
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /Users/liyuechun/Desktop/liyc_hello/package.json:

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


Is this ok? (yes) 
localhost:liyc_hello yuechunli$ 

创建index.js

function print_liyc_hello(name) {
        console.log(name);
}

exports.print_liyc_hello = print_liyc_hello;

新建测试文件test.js

var fn = require('./index.js');

fn.print_liyc_hello('大家好,我是春哥,我的电话是13331184066');

执行test

localhost:liyc_hello yuechunli$ pwd
/Users/liyuechun/Desktop/liyc_hello
localhost:liyc_hello yuechunli$ ls
index.js    package.json    test.js
localhost:liyc_hello yuechunli$ node test
大家好,我是春哥,我的电话是13331184066
localhost:liyc_hello yuechunli$ 

发布到NPM

去官网注册一个自己的开发账号

https://www.npmjs.com注册账号,记住自己的账号,密码,邮箱。

命令行连接NPM

localhost:liyc_hello yuechunli$ npm adduser
Username: liyuechun
Password: 
Email: (this IS public) liyuechun2009@163.com
Logged in as liyuechun on https://registry.npmjs.org/.
localhost:liyc_hello yuechunli$ npm whoami
liyuechun
localhost:liyc_hello yuechunli$ 

发布

localhost:liyc_hello yuechunli$ npm publish
+ liyc_hello@1.0.0
localhost:liyc_hello yuechunli$ 

使用

创建React 项目

localhost:Desktop yuechunli$ mkdir liycDemo
localhost:Desktop yuechunli$ cd liycDemo/
localhost:liycDemo yuechunli$ create-react-app testApp
Creating a new React app in /Users/liyuechun/Desktop/liycDemo/testApp.

Installing packages. This might take a couple minutes.
Installing react-scripts...

npm WARN prefer global marked@0.3.6 should be installed with -g

> fsevents@1.0.17 install /Users/liyuechun/Desktop/liycDemo/testApp/node_modules/fsevents
> node-pre-gyp install --fallback-to-build

[fsevents] Success: "/Users/liyuechun/Desktop/liycDemo/testApp/node_modules/fsevents/lib/binding/Release/node-v51-darwin-x64/fse.node" is installed via remote
testApp@0.1.0 /Users/liyuechun/Desktop/liycDemo/testApp
└─┬ react-scripts@0.9.2 
  ├─┬ autoprefixer@6.7.2 
  │ ├─┬ browserslist@1.7.5 
  │ │ └── electron-to-chromium@1.2.4 
  │ ├── caniuse-db@1.0.30000628 
  │ ├── normalize-range@0.1.2 
  │ ├── num2fraction@1.2.2 
  │ ├─┬ postcss@5.2.15 
  │ │ └── js-base64@2.1.9 
  │ └── postcss-value-parser@3.3.0 
Success! Created testApp at /Users/liyuechun/Desktop/liycDemo/testApp
Inside that directory, you can run several commands:

  npm start
    Starts the development server.

  npm run build
    Bundles the app into static files for production.

  npm test
    Starts the test runner.

  npm run eject
    Removes this tool and copies build dependencies, configuration files
    and scripts into the app directory. If you do this, you can’t go back!

We suggest that you begin by typing:

  cd testApp
  npm start

Happy hacking!

项目结构图

cover

安装 liyc_hello

localhost:liycDemo yuechunli$ ls
testApp
localhost:liycDemo yuechunli$ cd testApp/
localhost:testApp yuechunli$ atom ./
localhost:testApp yuechunli$ npm install --save liyc_hello
testApp@0.1.0 /Users/liyuechun/Desktop/liycDemo/testApp
└── liyc_hello@1.0.0 

localhost:testApp yuechunli$ 

安装后package.json内容变化

cover

联系我

微信&微信: 13331184066

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值