Ivin
Ivin

Reputation: 4815

Should we install react and react-dom in --save or --save-dev mode?

Some tutorials use --save mode while some use --save-dev mode.

Both of them use Babel, saying that it is used to convert React into plain Javascript that browsers understand. That means the final bundle will have Javascript. In that case why do we need to install react and react-dom in --save mode? --save-dev mode should suffice right?

Also, statements like import React from 'react', do they go into prod build bundle files? If not, how are statements like React.createClass work without React being available in that corresponding scope/memory-space?

Upvotes: 6

Views: 4231

Answers (1)

Sidney
Sidney

Reputation: 4775

--save mode is for packages that your app will use when it's running, like React. --save-dev are for packages that help you develop like linters, module bundlers, transpilers (eg Babel).

This separation is almost entirely for your benefit -- putting everything into your main dependencies with --save won't break anything, but it's considered bad organization.

By the way, NPM 5 and above will automatically save packages, so the --save flag isn't necessary anymore. (You still need to use --save-dev to mark packages as a "development dependency").

Also, statements like import React from 'react', do they go into prod build bundle files? If not, how are statements like React.createClass work without React being available in that corresponding scope/memory-space?

Yes, that import React from 'react' ends up in the bundle, but using a regular JavaScript function instead of an import statement. You can open up the bundle file in your text editor to see what your code has been transformed into.

Upvotes: 8

Related Questions