Joshua Lin (@konekoya)
This line refers to a separate JS file
Manually including your JS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>An intro to NPM and JavaScript Modules</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>And in your script.js you have:
Okay, this is all you need to make a website! 🎉🎉🎉
Wait... what if I need a third-party plugin or library like jQuery?
console.log('Hello world from script.js');
// logs out "Hello world from script.js" in your consoleDownloading jQuery
This is exactly what we want!
Adding jQuery in our example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>An intro to NPM and JavaScript Modules</title>
</head>
<body>
<script src="jquery-3.2.1.js"></script>
<script src="script.js"></script>
</body>
</html>Note that jQuery gets loaded before script.js
Now, in your script.js
console.log('Hello world from script.js');
$('body').append('Hello world from script.js');
// logs out 'Hello world from script.js' in the browser console and
// append the script to the body element of index.htmlTADA! The result is pretty, right?
Add this line
But...
What's npm anyway?
Yep, you have to install it first...
Installing Node.js also installs npm
Initializing a project with npm
npm initIt will create a package.json in your directory like this:
{
"name": "an-intro-to-npm-and-js-modules",
"version": "1.0.0",
"description": "",
"main": "jquery-3.2.1.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "konekoya <tank158r5155@yahoo.com.tw> (http://konekoya.github.io/)",
"license": "MIT"
}
Installing jQuery with npm
npm install jquery --saveThe install command does two things
fisrt, it downloads all the code from jquery package into a directory call node_modules
Second, it adds jQuery as a project dependency in the package.json file
{
"name": "an-intro-to-npm-and-js-modules",
"version": "1.0.0",
"description": "",
"main": "jquery-3.2.1.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "konekoya <tank158r5155@yahoo.com.tw> (http://konekoya.github.io/)",
"license": "MIT",
"dependencies": {
"jquery": "^3.2.1"
}
}
Okay, what's big deal?
Now, we can include jQuery from node_modules
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>An intro to NPM and JavaScript Modules</title>
</head>
<body>
<script src="node_modules/jquery/dist/jquery.min.js"></script>
<script src="script.js"></script>
</body>
</html>The output should be the same
But...
Modules for Node.js
It allows JavaScript to import and export code across files like most programming languages.
CommonJS
Let's use CommonJS to load JQuery, in our script.js
const $ = require('jquery');
console.log('Hello world from script.js');
$('body').append('Hello world from script.js');
But, if you open this in your browser. It won't work! 🐛 🐛
Because the require is not defined in the browser environment. It's design specifically for Node.js
Let's talk about webpack
Installing webpack
{
"name": "an-intro-to-npm-and-js-modules",
"version": "1.0.0",
"description": "",
"main": "jquery-3.2.1.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "konekoya <tank158r5155@yahoo.com.tw> (http://konekoya.github.io/)",
"license": "MIT",
"dependencies": {
"jquery": "^3.2.1"
},
"devDependencies": {
"webpack": "^3.8.1"
}
}
--save-dev flag tells npm to save this as devDependencies
This is what your package.json should look like now:
npm install webpack --save-devLet's build our little site with webpack
node_modules/.bin/webpack script.js bundle.jsAfter webpack finish building, you should have a new bundle.js in your folder
Remember to change your index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>An intro to NPM and JavaScript Modules</title>
</head>
<body>
<script src="bundle.js"></script>
</body>
</html>The output should be the same
Take a look of that HUGE bundle.js file
it's too big to include it in the slide...Create a webpack.config.js file in our root folder
module.exports = {
entry: './script.js',
output: {
filename: 'bundle.js',
},
};
node_modules/.bin/webpackRun the following command again, you should get the exact same result! webpack loads webpack.config.js automatically
But...
Let's change our CommonJS to ES modules
import $ from 'jquery';
console.log('Hello world from script.js');
$('body').append('Hello world from script.js');
Use webpack to build our site again
node_modules/.bin/webpack{
"name": "an-intro-to-npm-and-js-modules",
"version": "1.0.0",
"description": "",
"main": "jquery-3.2.1.js",
"scripts": {
"build": "webpack --progress -p",
"watch": "webpack --progress --watch"
},
"keywords": [],
"author": "konekoya <tank158r5155@yahoo.com.tw> (http://konekoya.github.io/)",
"license": "MIT",
"dependencies": {
"jquery": "^3.2.1"
},
"devDependencies": {
"webpack": "^3.8.1"
}
}
Let's add two npm scripts in our package.json
Add these two
Building our site with:
npm run watchWhen developing, use:
npm run buildwebpack will keep watching your file while you're working
when script.js
Add webpack-dev-server in our project
npm install webpack-dev-server --save-dev{
"name": "an-intro-to-npm-and-js-modules",
"version": "1.0.0",
"description": "",
"main": "jquery-3.2.1.js",
"scripts": {
"build": "webpack --progress -p",
"watch": "webpack --progress --watch",
"server": "webpack-dev-server --open"
},
"keywords": [],
"author": "konekoya <tank158r5155@yahoo.com.tw> (http://konekoya.github.io/)",
"license": "MIT",
"dependencies": {
"jquery": "^3.2.1"
},
"devDependencies": {
"webpack": "^3.8.1",
"webpack-dev-server": "^2.9.4"
}
}
In our package.json:
Add this line
Now, you can start a dev server with a single command
npm run serverOkay, a recap is necessary