Packaging Code Flashcards

1
Q

set a variable

A

variableName=”some value”
no spaces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

access a variable

A

echo $variableName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

set a variable before a command
What’s the lifetime of the variable

A

SOMETHING=”a value” env

The lifetime is just that one line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What happens with this:
$ SOMETHING=”something else” echo $SOMETHING

why?

A

SOMETHING not found
because variables are evaluated before the setting occurs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Change the prompt

A

PS1=’whatever’

PS1 is the variable for prompt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

turn a variable into a command

A

newVariable=”echo”

$newVariable ‘a string or whatever’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Mix 2 srings

MESSAGE1=”This is message 1.”
MESSAGE2=”This is message 2.”

A

MESSAGE=”$MESSAGE1 $MESSAGE2”
echo $MESSAGE
This is message 1. This is message 2.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens with this:

MESSAGE=’$MESSAGE1 $MESSAGE2’
echo $MESSAGE

A

$MESSAGE1 $MESSAGE2

To ensure that variables will be interpolated, you must use double quotation marks (“), not single quotes (‘). Try the following example in your command line to see the difference:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

A command is just a file.
How does the computer know where to look for that file?

A

If it doesn’t have /, ., or ~, it’s not a file name. It will then look in the paths of the PATH variable.

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

looks in sbin first, then bin etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Find what folder a command in is

A

which man
which ls

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Add a custom path but maintain the old ones

A

export PATH=”/path/to/my/executables-directory:$PATH”

So we’re setting path to our new option, then looks for the old options

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Users/groups have certain permissions to files
t or f

A

F
Files have access given to certain users/groups

There’s nothing about a person that inherently gives them permission to dine at a restaurant at a particular table and at a particular time. It’s because the restaurant has labeled, or set apart, that table for that time that the person is able to dine there. So it is with the file system. There’s nothing inherently special about any group or user (except the root user). A user’s privileges are defined by the files and directories themselves.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Do something as root

A

sudo command

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Enter root mode

A

sudo su

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what is the general node project directory structure

A

.git .gitignore in main path
any code files in lib directory
any test files in test directory
Then a directory called assets, with more directories for images, css, js

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to only assign 1 module to a variable (using require(‘’))

A

multiple independent files like lodash
const chunk = require(‘lodash/chunk’);

console.log(chunk([1, 2, 3, 4, 5, 6, 7, 8], 2));
// logs [[1, 2], [3, 4], [5, 6], [7, 8]]

otherwise
const chunk = require(‘lodash’).chunk;
This code doesn’t solve the processing issue; Node still needs to read the entire module. However, thanks to garbage collection, it doesn’t put much strain on memory resources; the imported but unneeded names are immediately eligible for garbage collection.

17
Q

What does the –save do in
npm install lodash –save

A

e save option tells npm to save the package to the dependencies list in package.json
can also do -S

18
Q

What does the –save-dev do in
npm install eslint –save-dev

A

Sometimes, your project only needs a package during development. Such packages include code linters, debuggers, and minifiers. These tools aren’t part of the final application but are useful or necessary during development. Ideally, you should only install such tools in the development environment, not in production. Fortunately, package.json lets you identify such development dependencies by adding a devDependencies property. The easiest way to do that is to use npm install with the –save-dev option

https://launchschool.com/lessons/3f433bfc/assignments/2f0f28c7

19
Q

what does npx do?

A

Since ESLint is an executable package, we’ll run it from the terminal. However, if you try to run eslint filename.js, it will use the globally installed eslint executable. The reason you install a package locally, though, is that you want to use that specific version of the package inside your project. Your globally installed version may not be compatible with your project for some reason.

There are several ways to run a local npm executable package, but the simplest way is to precede the executable’s name by npx:

The npx command uses the local version of eslint to scan the todolist.js file. Note that you don’t have to have the intended executable installed globally; npx merely checks for a local installation first. If it can’t find the package (eslint) locally or globally, it downloads and uses a temporary version of the named package.

20
Q

Delete a dependency
Delete a dependency and remove it from the package.json
Delete a dependecny and removes development dependencies.

A

npm uninstall lodash

npm uninstall lodash –save

npm uninstall eslint –save-dev

You can also use npm prune to remove dependencies. This command is useful when you manually remove some dependencies from package.json and want to remove the packages from node_modules. For example, suppose that we edit package.json and delete morgan from the dependencies list, then run npm prune. Since morgan is no longer a dependency, npm prune removes it from node_modules.

21
Q

How to install a package globally

A

add -g

22
Q

Why install packages locally?

A

As a general rule, you should install almost all of your packages locally in your project’s node_modules directory. We recommend this practice highly. It ensures that projects that need specific versions of packages have them locally available. If another project needs a different version of a package, you can install that version in its node_modules directory. Since each package is local to the project, each project is free to use the version it needs.

23
Q

What is transpilation?

A

Transpilation is the process of converting source code written in one language into another language with a similar level of abstraction to the original code. In the JavaScript world, it often means taking code written in a superset of JavaScript and rewriting it as plain JavaScript. Most often, that means converting code that uses the latest language features to an older version of the language. For instance, we may want to transpile a program written with ES6 features like let, class, and arrow functions into a form that lets it run in browsers that don’t recognize these features.

24
Q

Automate a task with an npm script

A

“scripts”: {
“test”: “echo "Error: no test specified" && exit 1”,
“foo”: “echo ‘How do you do?’”
},

25
Q

Do you always need npx for npm scripts?

"babel": "npx babel lib --out-dir dist --presets=@babel/preset-env"
A

“babel”: “babel lib –out-dir dist –presets=@babel/preset-env”

One significant difference with omitting npx from a script’s definition is that npx can search for and install packages for one-time execution. Without npx, you can only use pre-installed packages.

One useful feature of npm scripts is that npm knows how to find command-line executables, even those that are part of a local package. It uses commands from local packages in preference to those stored in more traditional locations, such as the directories specified by the PATH environment variable. Thus, you don’t need to use the npx command:

26
Q

When you’re ready to prepare your project for distribution, you should investigate the requirements for creating node modules. The steps required include…. (3 steps)

A

Create a package.json file.

Provide values for the name, version, and main fields:
name is the name of your package.
version is the initial module version.
main is the name of the file that Node will load when someone imports your package.

Publish your node package.

https://docs.npmjs.com/creating-node-js-modules

27
Q

What does npm prune do?

A

https://docs.npmjs.com/cli/v10/commands/npm-prune

28
Q

publish

A

npm publish –access public

29
Q

Difference between package and package-lock

A

Do you need both package-lock.json and package.json? No.

Do you need the package.json? Yes.

Can you have a project with only the package-lock.json? No.

The package.json is used for more than dependencies - like defining project properties, description, author & license information, scripts, etc. The package-lock.json is solely used to lock dependencies to a specific version number.

30
Q

What does everything in package.json and package-lock mean/do?

A

look up later

31
Q

Which of the following statements is true?
A
The contents of package.json and package-lock.json are identical. npm copies one to create the other.

B
npm uses package.json to determine which version node packages your project needs.

C
npm uses package.json in development environments and package-lock.json in production environments.

D
npm creates a package-lock.json file that contains your application’s dependency information.

A

BD

A: The contents of package.json and package-lock.json are not identical, though they are related.

C: package.json and package-lock.json are both used in development and production environments.

32
Q

How do you create a package-lock file?

A

npm install