Configuring for Node.js

In the interest of keeping the application as simple as possible, we'll avoid a build/bundling tool like Webpack or Rollup.js. This allows us to cut down on the number of required dependencies and ensures that any issues you run into aren't caused by a breaking change in a build dependency.

We'll create a Node.js project because it allows us to run scripts and install a dependency locally for development purposes. We've used the /book-examples folder up to this point, but we'll create a new project folder outside of /book-examples to configure a different default build task in VS Code. Open a terminal, cd into the desired folder, and enter the following commands:

// Create a new directory and cd into it:
mkdir cook-the-books
cd cook-the-books


// Create a package.json file with default values
npm init -y

The -y command forgoes the prompts and populates the package.json file with sensible defaults. Once completed, run the following command to install browser-sync:

npm install -D browser-sync@^2.24.4

The -D is optional and indicates that the library is a development dependency. You would use the -D flag if you were building and distributing the application, so I included it to adhere to common practice. I'd recommend installing that specific version to ensure the start script runs without any issues. After browser-sync installs, add the following entry to the scripts entry in the package.json file:

...
"scripts": {
...
"start": "browser-sync start --server "src" --files "src/**" --single --no-open --port 4000"
},
If you run npm init with the -y flag, there should be an existing script named test, which I omitted for clarity. If you didn't run it with the -y flag, you may need to create the scripts entry.

You can populate the "description" and "author" keys if desired. The file should end up looking similar to this:

{
"name": "cook-the-books",
"version": "1.0.0",
"description": "Example application for Learn WebAssembly",
"main": "src/index.js",
"scripts": {
"start": "browser-sync start --server "src" --files "src/**" --single --no-open --port 4000",
"test": "echo "Error: no test specified" && exit 1"
},
"keywords": [],
"author": "Mike Rourke",
"license": "MIT",
"devDependencies": {
"browser-sync": "^2.24.4"
}
}
If you omit the --no-open flag from the start script, the browser will open automatically. The flag was included to prevent issues with users running in a headless environment.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset